MySQL 将多个右连接行合并为一列

MySQL Contact multiple right joined rows into one column

如果我有这个

用户

id user

1 john   
2 tom

user_id state

1        MA
1        CA
2        VA

如何获得如下结果集:

id user states


1 john  MA,CA
2 tom   VA

我觉得跟群组联系有关系,但是我弄不出来

正如您所建议的那样,group_concat 是正确的选择 - 您只需要 group by users' 列:

SELECT   u.id, u.user, GROUP_CONCAT(s.state)
FROM     users u
JOIN     states s ON u.id = s.user_id
GROUP BY u.id, u.user