I'm trying to retrieve data from tables and combine multiple rows into a single column, without repeating any information.
I have the following tables: profile, qualification, projects.
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
I want to retrieve the all of the information for each person, with each person appearing only once in the result. The info on qualifications and projects should each be in their own column (one column for qualifications, another for projects), separated by commas. For example, the results for the above sample data should be:
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
Currently, I have the query:
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
For the sample data, this query results in:
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
Note: Raymond Luxury-Yachts isn't present in the current result.
I don't want duplicate result records. Also if the surname does not have any entry in the qualification and projects table, I want the query to return the name and display an empty field in the qualification and projects table instead of omitting them altogether.