在DELETE查询中绕过自引用

时间:2011-01-05 17:08:02

标签: mysql

我正在尝试删除所有不是其名称下最新版本的记录,但显然您无法在同一查询中引用正在修改的表。

我试过这个,但由于上述原因,它不起作用:

DELETE FROM table
WHERE CONCAT(name, version ) NOT IN (
SELECT CONCAT( name, MAX( version ) )
FROM table
GROUP name
)

我怎样才能解决这个问题?

干杯

1 个答案:

答案 0 :(得分:8)

Wrap the inner reference in a derived table

DELETE FROM table
WHERE  Concat(name, version) NOT IN (SELECT nv
                                     FROM   (SELECT Concat(name, Max(version))
                                                    AS nv
                                             FROM   table
                                             GROUP  BY name) AS derived)