带子查询的MySQL删除会产生语法错误

时间:2014-07-09 10:30:06

标签: mysql subquery sql-delete

在MySQL中,我有一个包含2个字段的表(' ertek'): ' id':int,autoincrement ' mert':int

我想要删除值为' mert'的行。如果我按id ASC订购表,则低于上一行。

我的查询:

DELETE FROM ertek as t WHERE EXISTS (SELECT * FROM ertek AS t2 WHERE t2.id=t.id-1 AND t2.mert>t.mert)

我收到此错误: 1064 - 您的SQL语法出错;检查与您的MySQL服务器版本相对应的手册,以获得正确的语法,以便在附近使用#as WHERE EXISTS(SELECT * FROM ertek AS t2 WHERE t2.id =(t.id)-1 AND t2.mert> t' at line 1

请帮忙。

1 个答案:

答案 0 :(得分:0)

尝试不使用已删除的表名的别名

DELETE FROM ertek D
WHERE EXISTS 
(SELECT * FROM ertek AS t2 WHERE t2.id=D.id-1 AND t2.mert>D.mert)

  DELETE d1 FROM ertek d1 JOIN ertek d2 
  ON d2.id= (d1.id-1) AND d1.mert< d2.mert;