Mysql根据特定列删除类似的行,但ID最高的行除外

时间:2018-10-14 10:13:10

标签: mysql

我的表在特定列中有重复的行值。我想删除这些行,并保留最新ID。 我要检查和比较的列是:

sub_id, spec_id, ex_time

所以,对于这张桌子

+----+--------+---------+---------+-------+
| id | sub_id | spec_id | ex_time | count |
+----+--------+---------+---------+-------+
|  1 |    100 |     444 | 09:29   |     2 |
|  2 |    101 |     555 | 10:01   |    10 |
|  3 |    100 |     444 | 09:29   |    23 |
|  4 |    200 |     321 | 05:15   |     5 |
|  5 |    100 |     444 | 09:29   |     8 |
|  6 |    101 |     555 | 10:01   |     1 |
+----+--------+---------+---------+-------+

我想得到这个结果

+----+--------+---------+---------+-------+
| id | sub_id | spec_id | ex_time | count |
+----+--------+---------+---------+-------+
|  5 |    100 |     444 | 09:29   |     8 |
|  6 |    101 |     555 | 10:01   |     1 |
+----+--------+---------+---------+-------+

根据此question

,我能够构建此查询以从多列中选择所有重复的行
select  t.*
from mytable t join
     (select id, sub_id, spec_id, ex_time, count(*) as NumDuplicates
      from mytable
      group by sub_id, spec_id, ex_time
      having NumDuplicates > 1
     ) tsum
     on t.sub_id = tsum.sub_id and t.spec_id = tsum.spec_id and t.ex_time = tsum.ex_time  

但是现在我不确定如何用删除查询来包装此选择,以删除ID最高的行。 为shown here

1 个答案:

答案 0 :(得分:1)

  • 您可以修改子选择查询,以获取每个重复组合的最大值id
  • 现在,在连接到主表时,只需设置一个条件,即id的值将不等于最大id的值。
  • 您现在可以从该结果集中Delete

尝试以下操作:

DELETE t 
FROM mytable AS t 
JOIN 
    (SELECT MAX(id) as max_id, 
            sub_id, 
            spec_id, 
            ex_time, 
            COUNT(*) as NumDuplicates
     FROM mytable
     GROUP BY sub_id, spec_id, ex_time
     HAVING NumDuplicates > 1
    ) AS tsum
     ON t.sub_id = tsum.sub_id AND 
        t.spec_id = tsum.spec_id AND 
        t.ex_time = tsum.ex_time AND 
        t.id <> tsum.max_id