Good day!
Let's say I have two tables defined as follows:
TABLE USER(id, name, ...other fields that are unimportant)
| id | name |
-------------
| 1 | John |
| 2 | Mary |
| 3 | Luke |
| 4 | Lisa |
TABLE CHOICE(id, uid, kname) where uid is the foreign key to table USER.
Please note: I do NOT know how many distinct kname entries the table CHOICE has. The only thing I know is that they will be limited (say between 0 and 10).
| id | uid | kname |
---------------------
| 1 | 1 | city1 |
| 2 | 1 | city2 |
| 4 | 2 | city2 |
| 5 | 2 | city3 |
| 6 | 4 | city4 |
Is it possible to write a query which returns this table:
| id | name | city1 | city2 | city3 | city4 |
---------------------------------------------
| 1 | John | 1 | 1 | 0 | 0 |
| 2 | Mary | 0 | 1 | 1 | 0 |
| 3 | Luke | 0 | 0 | 0 | 0 |
| 4 | Lisa | 0 | 0 | 0 | 1 |
i. e. the table USER extended with as many additional columns as distinct kname in table CHOICE, labelled with kname and containing a 1 if there exist a row in CHOICE with uid equals to the user id and a zero otherwise.