在GROUP_CONCAT中使用COUNT [英] Using COUNT in GROUP_CONCAT
本文介绍了在GROUP_CONCAT中使用COUNT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的表格:
id fk_company
1 2
2 2
3 2
4 4
5 4
6 11
7 11
8 11
9 12
我想要的结果应该是字符串3,2,3,1,因为这只是我的复杂查询字符串的一部分。
The result i want should be string "3, 2, 3, 1" because this is just part of my complex query string.
我试图使用这个查询:
SELECT GROUP_CONCAT(COUNT(id) SEPARATOR ", ")
FROM `table` GROUP BY fk_company;
但我遇到了错误:
错误号码:1111
无法使用群组功能
我有一种感觉COUNT,MAX,MIN或SUM不能在GROUP_CONCAT中使用。
I have a feeling COUNT, MAX, MIN or SUM can't be used in GROUP_CONCAT. If so, do you know another way to do this.
推荐答案
您需要 COUNT()
GROUP BY
You need to COUNT()
with GROUP BY
in an inner select first and then apply GROUP_CONCAT()
SELECT GROUP_CONCAT(cnt) cnt
FROM
(
SELECT COUNT(*) cnt
FROM table1
GROUP BY fk_company
) q
输出:
| CNT |
-----------
| 3,2,3,1 |
以下是 SQLFiddle 演示
Here is SQLFiddle demo
这篇关于在GROUP_CONCAT中使用COUNT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文