在mysql中连接值,其中一个表中的多行与另一个表中的一行相关,而不显示多行

时间:2012-03-25 00:41:19

标签: mysql database

我已经经历了几天的stackoverflow并且google了很多但仍然可以找到解决方案。 我有2个表格和资格 在表格配置文件中

  pro_id  surname    firstname   
--------  --------  ------------

   1  John James           
   2  King Fred        
   3  Paul Smith      
   4  Cindy Hayes

Qua_id  Degree   School   Year
------  ------   ------   -----
   1  MBA  Wharton university 2002          
   1  LLB  Yale University 2001    
   2  BSc  Covington University 1998
   2  BEd  Kellog University 1995
   2  Msc  MIT 2011
   3  MBA  Havard Business School 2002          
   3  MSc  Yale University 2012     
   4  BSc  University of Edinburgh 2010
   4  BA   University of Liverpool 2009    

现在我想要实现的是这个

1  John James MBA  Wharton university 2002,  LLB  Yale University 2001
2  King Fred  BSc  Covington University 1998, BEd  Kellog University 1995, Msc  MIT 2011
3  Paul Smith MBA  Havard Business School 2002, MSc  Yale University 2012  
4  Cindy Hayes BSc  University of Edinburgh 2010, BA   University of Liverpool 2009 

但我现在拥有的是:

1  John James MBA  Wharton university 2002  
1  John James LLB  Yale University 2001 
2  King Fred  BSc  Covington University 1998 BEd  
2  King Fred Kellog University 1995
2  King Fred Msc  MIT 2011
3  Paul Smith MBA  Havard Business School 2002
3  Paul Smith MSc  Yale University 2012  
4  Cindy Hayes BSc  University of Edinburgh 2010
4  Cindy Hayes BA   University of Liverpool 2009   

这是我的代码

 Select pro_id, surname, firstname concat(degree,school,year) as qual from profile,qualification Where profile.proid=qualification.qua_id

1 个答案:

答案 0 :(得分:0)

Select pro_id, surname, firstname, group_concat(degree,school,year) as qual
from profile
left join qualification
on qualification.qua_id = profile.pro_id
group by qua_id

你的归档名称看起来有点奇怪 - qua_id应该是profile_id(我认为shoudl实际上是user_id - 即它们应该是用户ID和用户表的外键)。