Using MySQL, if I have a table Topic
with the field who
that uses the primary key uid
from the table Users
:
| aid | subject | who |
---------------------------
| 1 | foo | 1, 2, 3 |
| 2 | bar | 1, 3 |
| 3 | spec | 1, 2, 4 |
| 4 | sauce | 3 |
and a table Users
:
| uid | fname | lname |
---------------------------
| 1 | Jack | Black |
| 2 | Jill | White |
| 3 | Tom | Grey |
| 4 | Dick | Brown |
and I want to query who is associated with each subject, by name, my result should look like:
| aid | subject | who
1 foo Jack Black, Jill White, Tom Grey
2 bar Jack Black, Tom Grey
3 spec Jack Black, Jill White, Dick Brown
4 sauce Tom Grey
I thought the query should look something like:
SELECT GROUP_CONCAT(namearray SEPARATOR ', ')
FROM
(SELECT CONCAT(fname, ' ', lname) AS namearray
FROM Users
WHERE uid IN (1,2,3) ORDER BY lname) AS tWho
which works when manually entering the 1,2,3 in the IN
clause. I don't even know where to begin to insert the field who
into the IN
clause.
Can this be done in one query? Or will I need to get the query results with the who
numbers, and perform a second query to get the final result?