I have 1 table with users in them like so:
id name email status
---------------------------------
1 Jane j@ne.do active
2 Jack j@ck.do active
3 Jane j@ne.do inactive
4 Jack j@ck.do inactive
I also have data table like so:
id user_id key value
------------------------------
1 1 firts_name Jane
2 1 last_name Do
3 2 city Amsterdam
4 2 first_name Jack
Now i want to have them like so in one query:
id name email status firts_name last_name city
-------------------------------------------------------------------------
1 Jane j@ne.do active Jane Do NULL
2 Jack j@ck.do active Jack NULL Amsterdam
3 Jane j@ne.do inactive NULL NULL NULL
4 Jack j@ck.do inactive NULL NULL NULL
is that even possible in one query?
Normaly i do 2 queries and then combine the 2 in php but i am curious if this is possible.
This can be done pretty easily by joining the tables and using
CASEstatements:Demo: http://www.sqlfiddle.com/#!2/66086/4