I have these two tables:
Table 1: tbl_bulkuploadfields
fid | categoryid | fieldname -------------------------------------- 1 | 1 | educationlevel 2 | 1 | institution_name 3 | 1 | course 4 | 1 | year_of_passing 5 | 1 | percentage
Table 2: tbl_bulkuploads
category | fieldname | fieldvalue ----------------------------------- 1 | 1 | SSC 1 | 2 | H.B.Kapadiya 1 | 3 | Primary school 1 | 4 | Mar-2000 1 | 5 | 89% 1 | 1 | HSC 1 | 2 | Vishwaniketan 1 | 3 | Higher Secondary 1 | 4 | Mar-2002 1 | 5 | 70%
Below is the structure which I want as a result of a pivot query:
category | educationlevel | institution_name | course | year_of_passing | percentage ------------------------------------------------------ 1 | SSC | H.B.Kapadiya | Primary School | Mar-2000 | 89% 1 | HSC | Vishwaniketan | Higher Secondary|Mar-2002 | 70%
I have written the below query but it is returning only a single row instead of multiple rows:
select distinct B.Category,
MAX(IF(tbl_bulkuploadfields.fieldname = 'educationlevel', B.fieldvalue, NULL)) AS educationlevel,
MAX(IF(tbl_bulkuploadfields.fieldname = 'institution_name', B.fieldvalue, NULL)) AS institution_name,
MAX(IF(tbl_bulkuploadfields.fieldname = 'course', B.fieldvalue, NULL)) AS course,
MAX(IF(tbl_bulkuploadfields.fieldname = 'year_of_passing', B.fieldvalue, NULL)) AS year_of_passing,
MAX(IF(tbl_bulkuploadfields.fieldname = 'percentage', B.fieldvalue, NULL)) AS percentage from tbl_bulkuploads B
inner join tbl_bulkuploadfields on tbl_bulkuploadfields.fid = B.fieldname
where B.Category = 1 GROUP BY B.Category