我正在尝试从表中获取不同的值。当我跑的时候select distinct count(id) from table
我得到了超过一百万的计数。但是如果我跑了select count(distinct id) from table
我只有大约 300k 的计数。这两个查询有什么区别?
Thanks
当你这样做时select distinct count(id)
那么你基本上在做:
select distinct cnt
from (select count(id) as cnt from t) t;
因为内部查询只返回一行,distinct
没有做任何事情。该查询统计了行数在表中(嗯,更准确地说,是行数id
is not null
).
另一方面,当你这样做时:
select count(distinct id)
from t;
然后查询计算不同值的数量id
出现在表中。这似乎就是您想要的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)