最經作項目時發現的問題,好像在update時也有。。。mysql
網上查到的資料以下:sql
1.使用mysql進行delete from操做時,若子查詢的 FROM 字句和更新/刪除對象使用同一張表,會出現錯誤。 對象
mysql> DELETE FROM tab1 WHERE col1 = ( SELECT MAX( col1 ) FROM tab1 );
ERROR 1093 (HY000): You can’t specify target table ‘tab1′ for update in FROM clause
針對「同一張表」這個限制,撇開效率不談,多數狀況下均可以經過多加一層select 別名表來變通解決,像這樣
DELETE FROM tab1
WHERE col1 = (
SELECT MAX( col1 )
FROM (
SELECT * FROM tab1
) AS t
);
或這樣ci
delete from theTable where id in
(
select id from
(
select min(id) id from theTable group by title HAVING count(*)>1
) ids
) ;get
------------------------------------------------------------------------
2. mysql delete from where in 時後面 的查詢語句裏不能加where條件
Sql代碼
delete from `t_goods` where fi_id in (select * from ( select fi_id from `t_goods` where fs_num is null and fs_name is null and fs_type is null and fs_using is null and fs_lifetime is null) b)
Sql代碼
delete from `t_goods` where fi_id in (select fi_id from `t_goods` where fs_num is null and fs_name is null and fs_type is null and fs_using is null and fs_lifetime is null)
Sql代碼
delete from `t_goods` where fi_id in ( select fi_id from `t_goods` )
上面三種狀況,只有中間的不能執行。
綜合起來就是mysql delete from where in 時後面 的查詢語句裏不能加where條件
---------------------------------------------------------------------------
3. delete from table... 這其中table不能使用別名
Sql代碼
delete from student a where a.id in (1,2);(執行失敗)
select a.* from student a where a.id in (1,2);(執行成功)it