mysql中delete from where子查詢的限制介紹

本篇文章給大家帶來的內容是關于mysql中delete from where子查詢的限制介紹,有一定的參考價值,有需要的朋友可以參考一下,希望對你有所幫助。

1、使用mysql進行delete from操作時,若子查詢的 FROM 字句和更新/刪除對象使用同一張表,會出現錯誤。(相關推薦:MySQL教程

mysql> DELETE FROM ‘tab’ where id in (select min(id) from tag GROUP BY field1,field2 HAVING COUNT(id)>1);
error: You can’t specify target table ‘tab’ for update in FROM clause.(不能為FROM子句中的更新指定目標表’tab’)

針對“同一張表”這個限制,多數情況下都可以通過多加一層select 別名表來變通解決,像這樣

DELETE FROM 'tab' where id in (     select id from      (         select max(id) from 'tab' GROUP BY field1,field2 HAVING COUNT(id)>1     ) ids );

2.delete from table… 這其中table不能使用別名

  • mysql> delete from table a where a.id in (1,2);(語法錯誤)

  • mysql> select a.* from table a where a.id in (1,2);(執行成功)

? 版權聲明
THE END
喜歡就支持一下吧
點贊13 分享