select可以取別名,delete不能。
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代碼
Sql代碼
Sql代碼
上面三種情況,只有中間的不能執行。
綜合起來就是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);(執行成功)
DELETE FROM v9_news WHERE title IN (SELECT title FROM (SELECT title FROM v9_news as newsb WHERE FROM_UNIXTIME(newsb.inputtime,'%Y-%m-%d')='2013-06-27' GROUP BY newsb.title HAVING COUNT(*) >1 ) b)
AND id NOT IN (SELECT id FROM (SELECT MIN(id) AS id FROM v9_news WHERE FROM_UNIXTIME(inputtime,'%Y-%m-%d')='2013-06-27' GROUP BY title HAVING COUNT(*)>1) c); TRUE!!
DELETE FROM v9_news AS news
WHERE news.title IN (SELECT title FROM v9_news as newsb WHERE FROM_UNIXTIME(newsb.inputtime,'%Y-%m-%d')='2013-06-27' GROUP BY newsb.title HAVING COUNT(*) >1 )
AND id NOT IN (SELECT MIN(id) FROM v9_news WHERE FROM_UNIXTIME(inputtime,'%Y-%m-%d')='2013-06-27' GROUP BY title HAVING COUNT(*)>1); FALSE!!