MySQL查询可以将行转换为列吗?

时间:2010-12-02 16:01:29

标签: mysql

我有许多表我试图与连接组合但是因此,结果以多行返回,而我希望将它们生成为新列。

member_information表

MemberID | FirstName | LastName 
---------------------------------
   1     |   John    |  Harris
   2     |   Sarah   |  Thompson
   3     |   Zack    |  Lewis

member_dependent_information表

MemberID | FirstName | LastName | Type
---------------------------------------
   1     |   Amy     | Harris   |  1 
   2     |   Bryan   | Thompson |  1
   2     |   Dewey   | Thompson |  2
   2     |   Tom     | Thompson |  2
   3     |   Harry   | Lewis    |  2
   3     |   Minka   | Lewis    |  1

MySQL查询:

SELECT
    t1.FirstName,
    t1.LastName,
    t1.MemberID,
    IF(t2.Type = '1',CONCAT(t2.FirstName,' ',t2.LastName),'') AS Spouse_Name,
    IF(t2.Type = '2',CONCAT(t2.FirstName,' ',t2.LastName),'') AS Child_Name,

FROM
    member_dependent_information t2
INNER JOIN
    member_information t1
USING
    (MemberID)
ORDER BY
    t1.LastName ASC,
    t1.MemberID ASC;

理想结果

MemberID | FirstName | LastName  | Spouse_Name    | Child_Name1   | Child_Name2 
--------------------------------------------------------------------------------
   1     |   John    |  Harris   | Amy Harris     |     NULL       |   NULL    
   2     |   Sarah   |  Thompson | Bryan Thompson | Dewey Thompson | Tom Thompson    
   3     |   Zack    |  Lewis    | Mika Lewis     | Harry Lewis    |   NULL

实际结果

MemberID | FirstName | LastName  | Spouse_Name    | Child_Name 
-------------------------------------------------------------------
   1     |   John    |  Harris   | Amy Harris     |    NULL
   2     |   Sarah   |  Thompson | Bryan Thompson |    NULL  
   2     |   Sarah   |  Thompson |      NULL      | Dewey Thompson  
   2     |   Sarah   |  Thompson |      NULL      | Tom Thompson    
   3     |   Zack    |  Lewis    | Mika Lewis     |    NULL      
   3     |   Zack    |  Lewis    |      NULL      | Harry Lewis    

当我的查询返回多行中的“正确”数据时,它不会根据需要将结果合并为一行。

下面已经提到了数据透视表/交叉表的建议,但我能够找到的每个参考建议使用数学计算或者要返回的字段数是已知的。我不会知道这个信息作为单个成员COULD有多达100个家属(虽然更像是4-8)

更新#1

我觉得我越来越接近最终解决方案了。我将函数GROUP_CONCAT添加到我的查询中,该查询返回单个列中的所有firstnames和单个列中的所有姓氏,但仍需要将它们分解为各自的列。

新功能是:

SELECT
  t1.MemberID,
  t1.FirstName,
  t1.LastName,
  GROUP_CONCAT(t2.FirstName) AS Dep_Firstnames,
  GROUP_CONCAT(t2.LastName) AS Dep_LastNames
FROM
  member_information t1
  LEFT OUTER JOIN member_dependent_information t2
    ON t1.MemberID = t2.MemberID
WHERE
  t1.Status = 1
GROUP BY
  t1.MemberID

3 个答案:

答案 0 :(得分:11)

有时解决问题的第一步是知道它叫什么。在那之后,这只是谷歌搜索的问题。您要创建的内容称为数据透视表交叉表报告。这是一个解释如何创建pivot tables in MySQL的链接。这里有更深入tutorial

更新:

现在您已经更新了问题,我对您要完成的工作有了更清晰的认识。我将根据MySQL的GROUP_CONCAT函数为您提供一个类似但不完全完全的替代解决方案。

select t1.FirstName, t1.LastName, group_concat(concat(t2.FirstName, ' ', t2.LastName))
from member_information as t1
left outer join member_dependent_information as t2 on t2.MemberID=t1.MemberID
group by t1.MemberID;

我已经验证了此查询,如下所示。首先是设置:

create table member_information (
    MemberID int unsigned auto_increment primary key,
    FirstName varchar(32) not null,
    LastName varchar(32) not null
) engine=innodb;

create table member_dependent_information (
    MemberID int unsigned not null,
    FirstName varchar(32) not null,
    LastName varchar(32) not null,
    Type int unsigned not null,
    foreign key (MemberID) references member_information(MemberID)
) engine=innodb;

insert into member_information (MemberID, FirstName, LastName) values
(1, 'John', 'Harris'),
(2, 'Sarah', 'Thompson'),
(3, 'Zack', 'Lewis');

insert into member_dependent_information (MemberID, FirstName, LastName, `Type`) values
(1, 'Amy', 'Harris', 1),
(2, 'Bryan', 'Thompson', 1),
(2, 'Dewey', 'Thompson', 2),
(2, 'Tom', 'Thompson', 2),
(3, 'Harry', 'Lewis', 2),
(3, 'Minka', 'Lewis', 1);

现在查询和结果:

mysql> select t1.FirstName, t1.LastName, group_concat(concat(t2.FirstName, ' ', t2.LastName))from member_information as t1
    -> left outer join member_dependent_information as t2 on t2.MemberID=t1.MemberID
    -> group by t1.MemberID;
+-----------+----------+------------------------------------------------------+
| FirstName | LastName | group_concat(concat(t2.FirstName, ' ', t2.LastName)) |
+-----------+----------+------------------------------------------------------+
| John      | Harris   | Amy Harris                                           | 
| Sarah     | Thompson | Bryan Thompson,Dewey Thompson,Tom Thompson           | 
| Zack      | Lewis    | Harry Lewis,Minka Lewis                              | 
+-----------+----------+------------------------------------------------------+
3 rows in set (0.00 sec)

答案 1 :(得分:1)

我不确定OP如何从programIDStatus转变为现在如何,但我能得到的最接近的是(不需要数据透视表) :

SELECT t1.MemberID,
    t1.FirstName,
    t1.LastName,
    concat(t2.FirstName, ' ', t2.LastName) as Spouse_Name,
    group_concat(concat(t3.FirstName, ' ', t3.LastName) ORDER BY t3.FirstName) as Children_names
FROM member_information t1
    LEFT JOIN member_dependent_information t2 ON (t1.MemberID=t2.MemberID AND t2.Type=1)
    LEFT JOIN member_dependent_information t3 ON (t1.MemberID=t3.MemberID and t3.Type=2)
GROUP BY MemberID;

产生:

+----------+-----------+----------+----------------+-----------------------------+
| MemberID | FirstName | LastName | Spouse_Name    | Children_names              |
+----------+-----------+----------+----------------+-----------------------------+
|        1 | John      | Harris   | Amy Harris     | NULL                        |
|        2 | Sarah     | Thompson | Bryan Thompson | Dewey Thompson,Tom Thompson |
|        3 | Zack      | Lewis    | Minka Lewis    | Harry Lewis                 |
+----------+-----------+----------+----------------+-----------------------------+

使用任何编程语言“轻松”将Children_names提取到单独的列中。

答案 2 :(得分:0)

这并不是您正在寻找的,但它可能是朝着正确方向迈出的一步。

SELECT 
     Table1.memberIDs, 
     Table1.firstname, 
     Table1.lastnames, 
     Table2.programIDs, 
     Table3.description 
FROM 
     Table1, 
     Table2, 
     Table3 
WHERE 
     Table1.memberIDs = Table2.memberIDs AND 
     Table2.programIDs = Table3.programID