简体   繁体   English

group_concat mysql子查询仅返回一项

[英]group_concat mysql subquery only returns one item

I have two tables, scoreTable and users. 我有两个表,scoreTable和用户。 I am trying to group by hash on scoreTable but selecting all the user names that have that hash. 我正在尝试按scoreTable上的哈希进行分组,但选择具有该哈希的所有用户名。 scoreTable only has the ids scoreTable只有ID

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' 对于上面的查询,我在userList中获得用户ID的列表: '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) 我想得到的是实际的“名称”而不是ID-名称在另一个表(用户)上

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). 但是由于某种原因,它仅显示一个用户(具有第一个ID的用户)。

Just join on the users table: 只需加入users表:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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