mysql计数唯一的行值

时间:2010-11-09 09:02:58

标签: mysql count distinct

TABLE quotation

id  clientid
1   25
2   25
3   25
4   25
5   26

如何查询TABLE quotation中存在多少个不同的客户?我不希望重复的条目被多次计算。

我需要答案为2,排成一行,因为唯一不重复的条目是(2526)。

5 个答案:

答案 0 :(得分:38)

select count(distinct clientid) from quotation

read more

答案 1 :(得分:2)

我找到了出路

SELECT COUNT(*) as total FROM (SELECT COUNT(*) FROM quotation GROUP BY
clientid) t1

答案 2 :(得分:2)

我在MySQL 5.x数据库上尝试了以下内容。

id是一个整数,clientid是一个整数。我填充了两行:

id  clientid
1   25
2   25

此SQL查询将打印具有两个元素的行:

select * from test1 group by clientid having count(*) = 2;

如果您需要2个或更多元素,请将上面示例中的= 2替换为>= 2

select * from test1 group by clientid having count(*) >= 2;

答案 3 :(得分:1)

如果要计算唯一条目的总数,这将返回列数中的数字。

SELECT COUNT(*) as total FROM (SELECT COUNT(*) FROM quotation GROUP BY clientid having count(*) > 1) t1

如果要计算具有重复行的条目总数,请使用以下mysql。

SELECT COUNT(*) as total FROM (SELECT COUNT(*) FROM quotation GROUP BY clientid having count(*) >= 2) t1

答案 4 :(得分:0)

SELECT clientid, COUNT(clientid) FROM quotation 
GROUP BY clientid