开发者

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 开发者_Go百科(
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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜