如何删除除MySQL表中最新日期之外的所有重复行?

时间:2013-01-10 09:47:05

标签: mysql sql duplicate-removal

  

可能重复:
  Remove duplicate rows from table with join

我的表看起来像这样:
abcddate
除了包含最新a的行之外,如何删除具有相同bcddate的所有行? 有一些行,ab相同,cd不相同。

2 个答案:

答案 0 :(得分:2)

试试这个:

DELETE FROM tableName 
WHERE (a, b, c, d, dte) NOT IN (SELECT a, b, c, d, dte 
                                FROM (SELECT a, b, c, d, MAX(dte) dte 
                                     FROM tableName GROUP BY a, b, c, d
                                ) AS A );

选中此SQL FIDDLE DEMO

答案 1 :(得分:0)

您可以像这样使用左表连接

DELETE table1
  FROM table1
  LEFT JOIN 
  ( SELECT MAX(`date`) as dates,a,b,c,d FROM table1
    GROUP BY a,b,c,d
  ) A
    ON table1.date = A.dates
   AND table1.a = A.a
   AND table1.b = A.b
   AND table1.c = A.c
   AND table1.d = A.d
 WHERE A.dates IS NULL;

See this SQLFiddle

有关详情,请参阅DELETE Syntax of MySQL