开发者

mysql count unique row values

TABLE quotation

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

How can I query how many different c开发者_StackOverflow社区lients exist in TABLE quotation? I don't want duplicate entries to be counted more than once.

I need the answer to be 2, in 1 row, because the only non-duplicated entries are (25, 26).


select count(distinct clientid) from quotation

read more


I find a way out

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


If you want to count the total number of unique entries this will return a number in column count.

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

If you want to count the total number of entries with duplicate rows then use the following mysql.

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


I tried the following on a MySQL 5.x database.

id is an integer and clientid is an integer. I populated with two rows:

id  clientid
1   25
2   25

This SQL query will print the rows that have exactly 2 elements:

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

If you want 2 or more elements, replace = 2 in the example above with >= 2.

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜