简体   繁体   中英

group_concat mysql subquery only returns one item

I have two tables, scoreTable and users. I am trying to group by hash on scoreTable but selecting all the user names that have that hash. scoreTable only has the ids

SELECT st.score,
GROUP_CONCAT(st.uid) as userList
FROM scoreTable st
GROUP BY hash

For the above query I get the list of user Ids in userList: '1,2,3,4'

What I would love to get is the actual "names" instead of the ids - the names are on another table (users)

SELECT st.score,
(SELECT group_concat(d.name) from users d d where d.uid = st.uid))
FROM scoreTable st
GROUP BY hash

But for some reason this only displays ONE user (the user with the first id).

Just join on the users table:

SELECT   st.score, group_concat(d.name)
FROM     scoreTable st
JOIN     users d ON d.uid = st.uid
GROUP BY hash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM