【问题标题】:MySQL Contact multiple right joined rows into one columnMySQL Concat 多个右连接行成一列
【发布时间】:2015-10-06 13:23:10
【问题描述】:

如果我有这个

用户

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

我认为这与群组联系有关,但我无法使其正常工作。

【问题讨论】:

    标签: mysql sql select group-concat


    【解决方案1】:

    正如你所建议的,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
    

    【讨论】:

    • 这似乎将所有状态都放在一个用户中。
    猜你喜欢
    • 2016-01-07
    • 2013-11-07
    • 2011-06-24
    • 1970-01-01
    • 2016-01-08
    • 2019-11-22
    • 2017-04-02
    • 1970-01-01
    • 2023-03-14
    相关资源
    最近更新 更多