我正在尝试从表中检索数据并将多行合并为一列,而不重复任何信息。
我有以下表格:简介、资格、项目。
Profile
pro_id surname firstname
------ ------- ----------
1 John James
2 King Fred
3 Luxury-Yachts Raymond
Qualification
pro_id Degree School Year
------ ------ ------ -----
1 MBA Wharton university 2002
1 LLB Yale University 2001
2 BSc Covington University 1998
2 BEd Kellog University 1995
Projects
pro_id Title Year
------ ------ ------
1 Social Networking 2003
1 Excavation of aquatic debris 2007
2 Design of solar radios 1992
2 Development of expert systems 2011
我想检索每个人的所有信息,每个人在结果中只出现一次。有关资格和项目的信息应分别位于各自的列中(一列用于资格,另一列用于项目),并以逗号分隔。例如,上述样本数据的结果应为:
1 John James MBA Wharton university 2002, LLB Yale University 2001 Social Networking 2003, Excavation of aquatic debris 2007, Design of Solar panels 2008
2 King Fred BSc Covington University 1998, BEd Kellog University 1995, Msc MIT 2011 Design of solar radios 1992, Development of expert systems 2011
3 Raymond Luxury-Yachts
目前,我有这样的疑问:
SELECT pro_id,
surname,
firstname,
group_concat(degree,school,year) AS qual,
concat(Title,year) AS work
FROM profile,
LEFT JOIN qualification
ON qualification.pro_id = profile.pro_id
JOIN projects
ON projects.pro_id = profile.pro_id
GROUP BY pro_id
对于示例数据,此查询结果为:
1 John James MBA Wharton university 2002, Social Networking 2003
1 John James LLB Yale University 2001, Excavation of aquatic debris 2007
1 John James MBA Wharton university 2002, Social Networking 2003, Excavation of aquatic debris 2007
etc
注意:Raymond Luxury-Yachts 未出现在当前结果中。
我不想要重复的结果记录。此外,如果姓氏在资格和项目表中没有任何条目,我希望查询返回姓名并在资格和项目表中显示一个空字段,而不是完全忽略它们。