繁体   English   中英

计算已被计数和分组的表的结果行

[英]Count result rows of table that is already counted and grouped by

我想计算计数结果并按表结果分组

像我有一个表, a

id |name
1  |abc
2  |abc
3  |abc
4  |xyz
5  |xyz 

我的查询是SELECT COUNT(id) as count_id from a GROUP BY name

...给出结果:

count_id
3
2

我想计算此结果的总行数为2

所以我的查询是SELECT COUNT(SELECT COUNT(id) as count_id from a GROUP BY name) as maincount from a

...但是它给了我phpmyadmin这个错误

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT count.....

这样做:

SELECT COUNT(DISTINCT name) FROM a

子选择将起作用,尽管可能更优雅。

  Select count(*) from (SELECT COUNT(id) as count_id from a GROUP BY name) as b 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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