MySQL在同一表上多个左联接用于分组结果

时间:2018-11-19 12:31:09

标签: mysql left-join

表格 This stable stores some history of address changes

Id  Name    Address        Group Id
1   AAA      Primary          1
2   BBB      Secondary        1
3   CCC      Primary          1
4   DDD      Secondary        1
5   EEE      Primary          1
6   FFF      Primary          2
7   GGG      Secondary        2
8   HHH      Primary          3
9   III      Secondary        4
10  JJJ      Secondary        1

结果 I need a result like beleow

Primary Address Secondary Address
AAA                  BBB
CCC                  DDD
EEE                  JJJ
FFF                  GGG
HHH                  NULL
NULL                 III

是否可以通过mysql joins达到此结果

1 个答案:

答案 0 :(得分:0)

您可以尝试使用case when表达式

select case when address='Primary' then name end as PrimaryAddress,
case when address='Secondary' then name end as SecondaryAddress
from tablename