Reputation: 57
I have this table 'results':
(name,score,state)
joe small, 10, NC
joe small, 5, MN
jane doe, 9, AL
I have this query:
SELECT name, avg(score)
FROM results
group by name
I would like to add a new result column of unique states (alphabetical order) related to name to each row of output like so:
joe small, 5, CA MN NV
jane doe, 7, AL CA FL
Would I do this with a nested select statement?
Upvotes: 0
Views: 86
Reputation: 219834
Use GROUP_CONCAT()
SELECT name,
avg(score),
GROUP_CONCAT(state SEPARATOR ' ')
FROM results
GROUP BY name
Upvotes: 4