I have tables like
tbl_biodata:
id : int value=>1
name : varchar(50) value => mike
...
tbl_biodata_education
first row—
id : int value=>1
biodata_id : foreignkey to tbl_biodata.id value => 1
level : varchar(50) value => bachelor
year : int(4) value => 2006
second row—
id : int value=>2
biodata_id : foreignkey to tbl_biodata.id value => 1
level : varchar(50) value => masters
year : int(4) value => 2010
I have to export data such that the biodata won’t repeat more than one time and all the
repeated education from tbl_biodata_education lists as bachelor 2006, masters 2010
The final table would be:
id
name => mike
education => bachelor 2006, masters 2010
Solution from simon-at-mso-net
SELECT
b.id,
b.name,
GROUP_CONCAT(be.education) AS education
FROM tbl_biodata b
LEFT JOIN (
SELECT
biodata_id,
CONCAT(level,' ',year) AS education
FROM tbl_biodata_education
) be
ON b.id = be.biodata_id
GROUP BY b.id
GROUP_CONCAT is your friend for getting the entries you need as a list, with 1 list per
tbl_biodata record. I opted to suggest a subquery to get the data fromtbl_biodata_educationsimply so that theeducationfield could beCONCATed more cleanly before beingGROUP_CONCATed in the outer query