[英]MYSQL 'select count(distinct col)' the result is wrong?
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? 因为钥匙?
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.