简体   繁体   English

使用不同的SQL查询在表中查找重复项会产生不同的结果

[英]Finding duplicates in table with different sql queries yields different results

I was expecting both queries to give the same outcome. 我期望两个查询都能给出相同的结果。 Why is this not the case? 为什么不是这样?

select count(*) as c,number from numbers group by number having count(*) > 1;

select * from numbers as a INNER JOIN numbers  as b 
                                    on a.number = b.number  and a.id < b.id; 

First query return 1661 results where as the second 1911. 第一个查询返回1661个结果,而第二个查询则返回1911。

Because the first query will always return one row per number. 因为第一个查询将始终为每个数字返回一行。
The second may return more rows with the same number (if you have triplicates etc) 第二个可能返回更多具有相同编号的行(如果您有重复项等)

try the below to see the difference 尝试以下以查看区别

SELECT a.number, count(*) FROM
(select * from numbers as a INNER JOIN numbers  as b 
   on a.number = b.number  and a.id < b.id) c
GROUP BY a.number
-- having count(*) > 1
-- ORDER BY COUNT(*) DESC

The first query is correct, showing you one row for each number. 第一个查询是正确的,为您显示每个数字一行。

But the second query shows you all combinations of equal numbers (see the fiddle). 但是第二个查询显示了所有相等的数字组合(请参见小提琴)。 If you have more than two records with the same number, the number of records when you JOIN will increase. 如果您有两个以上具有相同编号的记录,那么加入JOIN时的记录数量将增加。

If you have: 如果你有:

ID     NUMBER
1        1
2        1
3        1
4        2
5        3

Joining these two tables will result in three times the number 1 being displayed. 连接这两个表将导致显示数字1的三倍。

Here you can see a SQLFiddle and how your query works. 在这里,您可以看到SQLFiddle以及查询的工作方式。

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

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