计算已计数并按其分组的表的结果行

时间:2012-05-19 14:09:08

标签: mysql sql

我想计算计数和分组表结果的结果

就像我有一张桌子,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.....

2 个答案:

答案 0 :(得分:5)

这样做:

SELECT COUNT(DISTINCT name) FROM a

答案 1 :(得分:2)

虽然可能更优雅,但是子选择会起作用。

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