在连接某些值时将多个表中的数据转换为单行

时间:2012-03-30 06:53:14

标签: mysql sql database

我正在尝试从表中检索数据并将多行合并到一个列中,而不重复任何信息。

我有以下表格:个人资料,资格,项目。

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。

我不想要重复的结果记录。此外,如果姓氏在资格和项目表中没有任何条目,我希望查询返回名称并在资格和项目表中显示一个空字段,而不是完全省略它们。

3 个答案:

答案 0 :(得分:0)

用JOIN替换LEFT JOIN

Select pro_id, surname, firstname, group_concat(degree,school,year) as qual,concat(Title,year) as work 
from profile
join qualification on qualification.pro_id = profile.pro_id 
join projects on projects.pro_id = profile.pro_id group by pro_id

What is the difference between "INNER JOIN" and "OUTER JOIN"?

答案 1 :(得分:0)

使用Join将解决显示值的问题,即使项目表中没有记录也是如此。 对于第一个问题,您可以尝试创建存储函数并从select语句中调用它。此函数将pro_id作为参数,创建连接字符串并返回它。这是目前我能想到的唯一一个MySQL解决方案。

答案 2 :(得分:0)

我认为你对group_concat的想法非常接近。但是,如果可能没有值(因此保留空值),可能会导致问题。我希望每个辅助表都按人名进行预先连接,并加入到该结果中。消除空值问题

SELECT
      p.pro_id,
      p.surname,
      p.firstname,
      PreQConcat.UserQual,
      PrePJConcat.UserWork
   FROM 
      profile p
         LEFT JOIN 
            ( select q.pro_id,
                     group_concat( q.degree, q.school, q.year) AS UserQual
                 from
                     qualification q
                 group by
                     q.pro_id ) PreQConcat
            ON p.Pro_ID = PreQConcat.pro_id

         LEFT JOIN 
            ( select pj.pro_id,
                     concat(pj.Title, pj.year) AS UserWork
                 from
                     projects pj
                 group by
                     pj.pro_id ) PrePJConcat
            ON p.Pro_ID = PrePJConcat.pro_id

无论如何,你正在经历所有人,并希望所有他们各自的元素(当它们存在时)分组,那么为什么要分组它不存在的可能性。让JOINED查询每次运行一次,只有一个结果仅由其拥有数据的人分组,然后加入回原始个人资料。