1

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).

1
  • Put the GROUP BY in your subquery Commented Dec 20, 2013 at 20:06

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

@Cez The first query in the OP assumes this, I just carried on with this assumption
worked great thanks, now to figure out how to group_concat the name and id in a json format that won't break if name has double quotes...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.