简体   繁体   中英

How do I concat grouped rows as a single row in MySQL?

I have a query like SELECT CONCAT(manufacturer, ':', COUNT(*)) AS count FROM cars GROUP BY manufacturer which returns something like:

+---------+
| count   |
+---------+
| ford:10 |
| fiat:5  |
| kia:2   |
+---------+

Now I want to GROUP_CONCAT those rows into 1 row like:

+------------------------+
| result                 |
+------------------------+
| ford:10, fiat:5, kia:2 |
+------------------------+

But there's already a GROUP BY, so GROUP_CONCAT doesn't work. How can I achieve this with MySQL?

select Group_Concat(count SEPERATOR ',') from 
(SELECT CONCAT(manufacturer, ':', COUNT(*)) AS count
 FROM cars GROUP BY manufacturer) AS A

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