小编Hea*_*her的帖子

count(distinct col_name)与计算select distinct查询的行不同吗?

我在MySQL数据库中有一个包含1100万行的表.其中一列是个人ID号.人们在表格中列出了很多次,我想知道有多少个独特的个人ID号码.然后创建一个包含这些唯一数字的表格.当我从列中计算不同的个人ID号时,我获得的数字与将其直接插入表中时的数字不同.例如:

select count(distinct person_key) from big_table;  
Run Code Online (Sandbox Code Playgroud)

给了我一个4,074,890的计数.

然后当我尝试用它们创建一个表时,

insert into new_table select distinct person_key from big_table; 
Run Code Online (Sandbox Code Playgroud)

它只创建2,701,875行.

(另外,如果我使用查询:select count(1) from (select distinct person_key from big_table) temp;它给了我2,701,875.)

我有什么想法我做错了吗?

mysql distinct

5
推荐指数
1
解决办法
192
查看次数

标签 统计

distinct ×1

mysql ×1