简体   繁体   中英

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?

Check the GROUP_CONCAT function. The query would be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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