Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find duplicate rows based on multiple fields in MySQL? [duplicate]

Tags:

sql

mysql

Possible Duplicate:
Mysql Duplicate Rows ( Duplicate detected using 2 columns )

In a MySQL database I have many rows. For example:

id | title   | time  | domain 32   title1    12:30   domain1.com 33   title1    12:30   domain2.com 34   title2    14:20   domain1.com 35   title3    14:30   domain2.com 36   title1    12:30   domain55.com 

How am I able to select rows from a database based on only title and time? Duplicate domains or ID's are not of concern, only the other two fields.

I want to be able to retrieve rows 32, 33 and 36 because they have identical titles and identical times.

I don't want to have to put in a title or time, I want the query to return all fields where there's a "duplicate" match found on these two fields whether that be only two or 50. That way I can go through and edit or delete some of the duplicates.

like image 679
Codexer1912 Avatar asked Oct 02 '12 13:10

Codexer1912


People also ask

How do I find duplicate values in mysql?

Find duplicate values in one column First, use the GROUP BY clause to group all rows by the target column, which is the column that you want to check duplicate. Then, use the COUNT() function in the HAVING clause to check if any group have more than 1 element. These groups are duplicate.


2 Answers

Here is what you want

SELECT title, time     FROM table GROUP BY title, time   HAVING count(*) > 1 
like image 164
kasi Avatar answered Sep 22 '22 23:09

kasi


select distinct id, title, time   from table t1  where exists (select *                   from table t2                  where t2.id <> t1.id                   and t2.title = t1.title                   and t2.time = t1.time                ) 
like image 20
Wolfgang Avatar answered Sep 20 '22 23:09

Wolfgang