/*
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;