简体   繁体   English

mysql查询,用于根据时间戳识别和删除重复项

[英]mysql query to identify and delete duplicates based on timestamp

I am trying to build a mysql query to list all column a's that have a duplicate column b from a single table. 我正在尝试构建一个mysql查询,以列出从单个表中具有重复列b的所有列a。 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. SELECT语句返回cola = colb记录,以及其他具有较晚日期的匹配行。 The DELETE statement deletes all records returned by the SELECT. DELETE语句删除SELECT返回的所有记录。

If you're looking to remove duplicate cola , then this is the query: 如果您要删除重复的cola ,那么这是查询:

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? 上面的查询将返回所有重复项。这是你在寻找什么?

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

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