我的表看起来像这样:
a
,b
,c
,d
,date
除了包含最新a
的行之外,如何删除具有相同b
,c
,d
和date
的所有行?
有一些行,a
和b
相同,c
和d
不相同。
答案 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 );
答案 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;
有关详情,请参阅DELETE
Syntax of MySQL