There's a following table, called fields:
And there's a dedicated table to store its values, called values
I want to run a query to produce the following output:
Finished | Faculity | Characteristic | Photo
---------------------------------------------
1 | Math | Good |
0 | Biology | Not Good |
I want to build a query that outputs aformentioned result. But it's not that easy as it seems. From this simlar question, I have tried running the following query:
SELECT flds.id,
(case when flds.name = 'Finished' THEN vals.value END) AS Finished,
(case when flds.name = 'Faculty' THEN vals.value END) AS Faculty,
(case when flds.name = 'Characteristic' THEN vals.value END) AS Characteristic,
(case when flds.name = 'Photo' THEN vals.value END) AS Photo
FROM `values` vals
LEFT JOIN `fields` flds
ON vals.field_id = flds.id
GROUP BY
flds.id,
vals.value;
Which gives me an unexpected result:
Is there any way to resolve it?



count the number of field values prior to the current id then group by and aggregate eg