mysql 计算唯一行值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4131937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:40:40  来源:igfitidea点击:

mysql count unique row values

mysqlcountdistinct

提问by love

TABLE quotation

TABLE quotation

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

How can I query how many different clients exist in TABLE quotation? I don't want duplicate entries to be counted more than once.

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

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

我需要的答案是2,在 1 行中,因为唯一的非重复条目是 ( 25, 26)。

回答by Haim Evgi

select count(distinct clientid) from quotation

read more

阅读更多

回答by octopusgrabbus

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

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

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

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

id  clientid
1   25
2   25

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

此 SQL 查询将打印恰好具有 2 个元素的行:

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

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

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

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

回答by Carl Schulz

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.

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

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

回答by love

I find a way out

我找到出路

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

回答by Gajendra Bang

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