繁体   English   中英

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

[英]Delete using WHERE clause from the rows of the same table 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
);

为此,我得到一个错误:

错误代码:1093。您无法在FROM子句中指定目标表'image_shout'进行更新

在Mysql中,您不能从要删除的表中选择。 但是您可以使用另一个子查询来欺骗它。

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
)

或者直接在删除语句中使用联接

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