2

我想统计counted和group by table结果的结果

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

4

2 回答 2

5

只需这样做:

SELECT COUNT(DISTINCT name) FROM a
于 2012-05-19T14:15:02.087 回答
2

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

  Select count(*) from (SELECT COUNT(id) as count_id from a GROUP BY name) as b 
于 2012-05-19T14:14:53.877 回答