简体   繁体   English

使用WHERE子句从MySQL同一表的行中删除

[英]Delete using WHERE clause from the rows of the same table in MySQL

I'm having rows in the table which I intend to delete in MySQL: 我表中有要在MySQL中删除的行:

delete from image_shout 
where auto_id in 
(
  select s.auto_id 
  from image_shout s 
  left join images i on s.image_id = i.image_id
  where i.image_id is null
);

For doing this I get an error: 为此,我得到一个错误:

Error Code: 1093. You can't specify target table 'image_shout' for update in FROM clause 错误代码:1093。您无法在FROM子句中指定目标表'image_shout'进行更新

In Mysql you can't select from a table you are deleting from. 在Mysql中,您不能从要删除的表中选择。 But you can trick it with another subquery. 但是您可以使用另一个子查询来欺骗它。

delete from image_shout 
where auto_id in 
(
   select * from 
   (
     select s.auto_id 
     from image_shout s 
     left join images i on s.image_id = i.image_id 
     where i.image_id is null
   ) tmp_tbl
)

Or use a join directly in the delete statement 或者直接在删除语句中使用联接

delete s 
from image_shout s
left join images i on s.image_id = i.image_id 
where i.image_id is null

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

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