I have trouble explaining my problem in words. so thats why my table examples:
let say i want a result table like:
user_id |data_a |data_b
1 |hello |world
and i need to extract it out of the table “dataset” in this form
data_id |user_id|data_type |data_data
1 |1 |data_a |hello
2 |1 |data_c |xxx
3 |2 |data_a |xxx
4 |1 |data_b |world
I do somehting like
SELECT user_id , data_a, data_b
FROM dataset
WHERE ( data_type = data_a OR data_type = data_b )
AND user_id = 1
AND a = ( SELECT data_data WHERE data_type = data_a )
AND b = ( SELECT data_data WHERE data_type = data_b )
however it is not working. i know i am doing it wrong but i dont know how to do it and how it is called. I think a privot table but i am not sure
Thats why the description is so vague i hope someone understands.
This is basically a
PIVOTbut MySQL does not have aPIVOTfunction so you can replicate it using an aggregate function and aCASEstatement. So your SQL will be something like this:See SQL Fiddle with Demo
Result: