GROUP_CONCAT与两个表的LEFT OUTER JOIN

时间:2014-05-29 15:43:44

标签: php sql left-join group-concat

SELECT table1.value, table2.additionalinfo
FROM table1
LEFT OUTER JOIN table2
ON table1.ID=table2.ID

我尝试输出类似这样的东西

Value   AdditionalInfo
blah    something, somethingelse, more
blah2   NULL
blah3   stuff

但我得到

Value   AdditionalInfo
blah    something
blah    somethingelse
blah    more
blah2   NULL
blah3   stuff

我尝试使用GROUP_CONCATGROUP_CONCAT(DISTINCT)

SELECT GROUP_CONCAT(table1.value), table2.additionalinfo
FROM table1
left outer JOIN table2
ON table1.ID=table2.ID ORDER BY table1.value

当我添加GROUP_CONCATORDER BY table1.value时,它会为每个值列出一个附加信息,但没有值,也没有重复的附加信息。我四处移动订单都无济于事。

1 个答案:

答案 0 :(得分:0)

我认为您需要在GROUP_CONCAT列上AdditionalInfo而不是table1.value

SELECT table1.value, 
GROUP_CONCAT(distinct table2.additionalinfo) additionalinfo
FROM table1
left outer JOIN table2 ON table1.ID=table2.ID 
GROUP BY table1.value
ORDER BY table1.value