Question: I'm having difficulty with a simple while loop. 

```
size(500,500);
background(255);
rectMode(CENTER);
noCursor();

int x1 = mouseX;
int y1 = mouseY;
int count = 0;
fill(0);
rect(250,250,50,50); //this is drawn
while (count == 0); {
  fill(0);
  rect(250,250,50,50); //this is not
}

```

Its written in Processing.
Anything I have in the while loop isn't drawn, while anything after it isn't either. Anything before it, not in a loop, is drawn. What am I missing here?

Answer: 
```
SELECT p.*
     , group_concat(CASE f.name WHEN 'telephone' THEN fp.value END) telephone
     , group_concat(CASE f.name WHEN 'address'   THEN fp.value END) address
  FROM person p
  JOIN field f
    LEFT JOIN fieldPerson fp
      ON    fp.personId = p.id
        AND fp.fieldId = f.id
  GROUP BY p.id

```
So, depending on how much control you have over the query generation, if you can generate the `SELECT` columns in the same order as the `PRIMARY KEY` of `field`, you could try the following. I'm envisioning something where you know there's 2 keys in `field`, and what they are in advance (from an earlier query?).

```
SELECT fp.personId,p.name,p.email,fp.value as telephone,fp2.value as Address 
    FROM fieldPerson fp INNER JOIN person p ON p.id = fp.personId 
                        INNER JOIN field f ON f.id = fp.fieldId 
                        INNER JOIN fieldPerson fp2 
    WHERE fp.fieldId = 1 and fp2.fieldId = 2;

```

This gives me the result, given the sample data in the question:

```
personId | name   | email           | telephone      | Address   |
       1 | Test 1 | test1@gmail.com | +1 555 555 555 | First St. |

```

This hinges on the notion that you've avoided the sub-query by doing an initial query earlier to know the arbitrary fields defined in the `fields` table. You didn't specify how dynamic this table is expected to be, so this may not be an unreasonable simplifying assumption to make.

Barring that, you can try something like:

```
SELECT fp.personId,p.name,p.email,fp.value as telephone,fp2.value as Address 
    FROM fieldPerson fp INNER JOIN person p ON p.id = fp.personId 
                        INNER JOIN field f ON f.id = fp.fieldId 
                        INNER JOIN fieldPerson fp2 
    WHERE fp.fieldId != fp2.fieldId LIMIT 1,1;

```

This gives me the same output without having to know the field id numbers, but I do need to know the names of the fields to generate the column names. The `LIMIT` is there to remove one of the `JOIN` products, and has a distinctly unholy feeling to it, I agree. I feel like there's not too much advantage to approach 2, since you have to know the column names anyways. 

I would agree with @Mojtaba, however, that if you can't live with a pre-query to know the dynamic field names and ids, then I believe you're going to have to put sub-`SELECT` statements in there to pull those at query time.