如何在MySQL中将分组行连接为单行?

时间:2011-04-15 19:19:57

标签: mysql group-concat

我有一个像SELECT CONCAT(manufacturer, ':', COUNT(*)) AS count FROM cars GROUP BY manufacturer这样的查询,它返回的内容如下:

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

现在我想把这些行GROUP_CONCAT分成1行,如:

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

但是已经有了GROUP BY,所以GROUP_CONCAT不起作用。我怎样才能用MySQL实现这个目标?

1 个答案:

答案 0 :(得分:3)

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