簡體   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