sql 在多列中查找重复项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 在多列中查找重复项相关的知识,希望对你有一定的参考价值。

/*
col1 | col2
-----+-----
111  | 222
333  | 444
111  | 333
555  | 111

To get a count of each element use UNION ALL in a subquery, then GROUP BY on the result of that.

Add HAVING COUNT(*) > 1 if you only wish to see the duplicated values.
*/

SELECT col, COUNT(*)
FROM
(
    SELECT col1 AS col FROM Table1
    UNION ALL
    SELECT col2 FROM Table1
) T1
GROUP BY col;
/*
http://stackoverflow.com/questions/854128/find-duplicate-records-in-mysql

Result:
JIM    JONES    100 MAIN ST
JOHN   SMITH    100 MAIN ST
*/

SELECT firstname, lastname, table_name.address FROM table_name
INNER JOIN (SELECT address FROM table_name
GROUP BY address HAVING count(id) > 1) show_dupes ON table_name.address = show_dupes.address;

/*
Instead of:
SELECT address, count(id) as cnt FROM table_name
GROUP BY address HAVING cnt > 1;

100 MAIN ST    2
*/
-- http://stackoverflow.com/questions/16324328/mysql-select-records-for-duplicates-using-multiple-columns

-- If you want to count duplicates among multiple columns, use group by:

select ColumnA, ColumnB, ColumnC, count(*) as NumDuplicates
from table
group by ColumnA, ColumnB, ColumnC;

-- If you only want the values that are duplicated, then the count is 
-- bigger than 1. You get this using the having clause:

select ColumnA, ColumnB, ColumnC, count(*) as NumDuplicates
from table
group by ColumnA, ColumnB, ColumnC
having NumDuplicates > 1;

-- If you actually want all the duplicate rows returns, then join the last 
-- query back to the original data:

select t.*
from table t join
     (select ColumnA, ColumnB, ColumnC, count(*) as NumDuplicates
      from table
      group by ColumnA, ColumnB, ColumnC
      having NumDuplicates > 1
     ) tsum
     on t.ColumnA = tsum.ColumnA and t.ColumnB = tsum.ColumnB and t.ColumnC = tsum.ColumnC;

-- This will work, assuming none of the column values are NULL. If so, then try:

     on (t.ColumnA = tsum.ColumnA or t.ColumnA is null and tsum.ColumnA is null) and
        (t.ColumnB = tsum.ColumnB or t.ColumnB is null and tsum.ColumnB is null) and
        (t.ColumnC = tsum.ColumnC or t.ColumnC is null and tsum.ColumnC is null)
/*
Select statement to find duplicates on certain fields

http://stackoverflow.com/questions/4434118/select-statement-to-find-duplicates-on-certain-fields

To get the list of fields for which there are multiple records, you can use..
*/

select field1,field2,field3, count(*)
  from table_name
  group by field1,field2,field3
  having count(*) > 1;

以上是关于sql 在多列中查找重复项的主要内容,如果未能解决你的问题,请参考以下文章

Excel多列中查找重复的数据

SQL:使用案例在单独的列中查找重复项和标记

EXCEL把多列数据变成一行的函数

在sql中使用exists来查找重复项,有没有更干净的方法?

MS SQL - 查找和删除重复项[重复]

mysql 多列数据搜索出不重复的各项