delete from TableA where id in (select * from (SELECT id from TableA where col_name is null) b )
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
);
------------------------------------------------------------------------
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);(執行成功)
@import url(http://www.shnenglu.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
posted on 2017-03-29 17:47
聶文龍 閱讀(205)
評論(0) 編輯 收藏 引用 所屬分類:
Database