1.使用mysql進行delete from操作時,若子查詢的 FROM 字句和更新/刪除物件使用同一張表,會出現錯誤。
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);(執行成功)