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

虽然我的查询在多行中返回“正确”数据,但并没有根据需要将结果合并为一行。

下面已经提到了有关数据透视表/交叉表的建议,但是我能够找到的每个参考都建议使用数学计算,或者知道要返回的字段数。我将不知道此信息,因为单个成员最多可能有100个受抚养人(尽管更像是4-8)

更新1

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

新功能是:

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

回答:

有时,解决问题的第一步是知道它的名字。在那之后,这只是谷歌搜索的问题。您尝试创建的内容称为 或

。这是解释如何在MySQL中创建数据透视表的链接。这是更深入的教程。

更新:

现在,您已经更新了问题,对于您要完成的工作,我有了更清晰的认识。我将为您提供一个替代解决方案,该解决方案与基于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)

以上是 MySQL查询可以将行变成列吗? 的全部内容, 来源链接: utcz.com/qa/423132.html

回到顶部