简体   繁体   English

在DELETE查询中绕过自引用

[英]Get around self-referencing in a DELETE query

I'm trying to delete all records which aren't the latest version under their name but apparently you can't reference access a table you are modifying in the same query. 我正在尝试删除所有不是其名称下的最新版本的记录,但显然您无法在同一查询中引用正在修改的表。

I tried this but it doesn't work for the reasons above: 我试过这个,但由于上述原因,它不起作用:

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

How can I get around this? 我怎么能绕过这个?

Cheers 干杯

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)  
delete t1
from  table_name1 t1, table_name1 t2 
where t1.version < t2.version 
and t1.name = t2.name;

//creating alias is the need here //这里需要创建别名

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

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