我有三个表:JOIN和GROUP_CONCAT三个用表
users: sports: user_sports:
id | name id | name id_user | id_sport | pref
---+-------- ---+------------ --------+----------+------
1 | Peter 1 | Tennis 1 | 1 | 0
2 | Alice 2 | Football 1 | 2 | 1
3 | Bob 3 | Basketball 2 | 3 | 0
3 | 1 | 2
3 | 3 | 1
3 | 2 | 0
表user_sports
users
链接和sports
具有优先的顺序(pref
)。
我需要返回的查询:
id | name | sport_ids | sport_names
---+-------+-----------+----------------------------
1 | Peter | 1,2 | Tennis,Football
2 | Alice | 3 | Basketball
3 | Bob | 2,3,1 | Football,Basketball,Tennis
我试图与JOIN
和GROUP_CONCAT
但我得到奇怪的结果。
我需要做一个嵌套查询吗?
任何想法?
你可以发布你的group_concat例程不起作用吗?这似乎正是你所需要的(见本页:http://stackoverflow.com/questions/276927/can-i-concatenate-multiple-mysql-rows-into-one-field),所以它可能会有所帮助查看查询,确定它出错的方式。 – user158017