Rio
Rio

Reputation: 14902

Selecting and aggregating multiple rows in MySQL

I have a query that returns several rows that I'd like to group by a column. So for example,

title  | label
--------------
may      ny
may      ct
june     ma
june     ri

How would I (efficiently) return

title  | label
--------------
may      ct,ny
june     ma,ri

such that the ordering of the concatenation is alphabetical?

Upvotes: 1

Views: 21

Answers (1)

Mosty Mostacho
Mosty Mostacho

Reputation: 43494

Check the GROUP_CONCAT function. The query would be:

SELECT title, GROUP_CONCAT(label ORDER BY label)
GROUP BY title

Upvotes: 2

Related Questions