程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> mysql中You can’t specify target table for update in FROM clause錯誤解決方法

mysql中You can’t specify target table for update in FROM clause錯誤解決方法

編輯:關於MYSQL數據庫

mysql中You can't specify target table for update in FROM clause錯誤的意思是說,不能先select出同一表中的某些值,再update這個表(在同一語句中)。 例如下面這個sql:
復制代碼 代碼如下:
delete from tbl where id in
(
        select max(id) from tbl a where EXISTS
        (
            select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1
        )
        group by tac
)

改寫成下面就行了:

復制代碼 代碼如下:
delete from tbl where id in
(
    select a.id from
    (
        select max(id) id from tbl a where EXISTS
        (
            select 1 from tbl b where a.tac=b.tac group by tac HAVING count(1)>1
        )
        group by tac
    ) a
)

也就是說將select出的結果再通過中間表select一遍,這樣就規避了錯誤。注意,這個問題只出現於mysql,mssql和oracle不會出現此問題。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved