我有三個表: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