简体   繁体   English

MYSQL'选择计数(distcolt col)'结果错误?

[英]MYSQL 'select count(distinct col)' the result is wrong?

This is my table 这是我的桌子

CREATE TABLE `tabletest`     
( `id` bigint(20) NOT NULL ,     
`type` int(11) NOT NULL DEFAULT '0' ,     
`parent_id` bigint(20) NOT NULL DEFAULT '0' ,    
 PRIMARY KEY (`id`),     
KEY `idx_parent_id_type` (`parent_id`,`type`)     
) ENGINE=InnoDB DEFAULT CHARSET=utf8

data: 数据:

type   parent_id    
101    0    
101    4    
101    6    
101    7    
101    9

this is sql 1: 这是SQL 1:

select count(DISTINCT parent_id) from tabletest where type = 101

the result 1: 结果1:

3

this is sql 2: 这是SQL 2:

select count(DISTINCT parent_id,type) from tabletest where type = 101

the result 2: 结果2:

5

why? 为什么? because the key? 因为钥匙?

It's a known MySQL bug, you could check it via other engines (spark sql or something). 这是一个已知的MySQL错误,您可以通过其他引擎(spark sql等)进行检查。 Try changing your MySQL version. 尝试更改您的MySQL版本。 See this 看到这个

Well, I replicate your test with your code and dataset and my results is (under MariaDB5.5.52) : 好吧,我用您的代码和数据集复制了您的测试,我的结果是(在MariaDB5.5.52下):

select count(DISTINCT parent_id) from tabletest where type = 101;

->5 -> 5

select count(DISTINCT parent_id,type) from tabletest where type = 101

->5 -> 5

What is your version of MySQL ? 您的MySQL版本是什么?

尝试这个 :

SELECT DISTINCT(A.type),COUNT(*) AS CountNumber FROM tabletest AS A WHERE A.type=101 GROUP BY A.type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM