I am trying to build a mysql query to list all column a's that have a duplicate column b from a single table. The trick is I have a timestamp on the rows so i need to essentially identify which is the older of the duplicates so i can delete it. Any help would be appreciated.
Just example - this query return duplicate posts, now you just need to execute delete
id| title | text_desc | created
-------------------------------------------------------
1 | The title | description here |2012-02-21 10:58:58
2 | The title | description here 1 |2012-02-21 10:58:58
3 | The title | description here 3 |2012-02-21 10:58:58
select bad_rows.*
from posts as bad_rows
inner join (
select title, MIN(id) as min_id
from posts
group by title
having count(*) > 1
) as good_rows on good_rows.title = bad_rows.title
and good_rows.min_id <> bad_rows.id;
Here is the return rows
id| title | text_desc | created
-------------------------------------------------------
2 | The title | description here 1 |2012-02-21 10:58:58
3 | The title | description here 3 |2012-02-21 10:58:58
Here's your query:
DELETE FROM tablename
WHERE id IN
(SELECT t1.id
FROM tablename t1
JOIN tablename t2
ON t2.cola = t1.cola AND t2.colb = t1.colb
AND t2.timecol > t1.timecol
WHERE t1.cola = t1.colb)
The SELECT statement returns records where cola = colb
and there are other matching rows with a later date. The DELETE statement deletes all records returned by the SELECT.
If you're looking to remove duplicate cola
, then this is the query:
DELETE FROM tablename
WHERE id IN
(SELECT t1.id
FROM tablename t1
JOIN tablename t2
ON t2.cola = t1.cola
AND t2.timecol > t1.timecol)
SELECT FOOCODE,COUNT(*) AS DUPS
FROM TABLE
GROUP BY FOOCODE
HAVING COUNT(FOOCODE)>1;
The above query will return u all the duplicates.Is this what u are looking for?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.