mysql中Youcan’tspecifytargettableforupdateinFROMclau_MySQL
來源:懂視網
責編:小采
時間:2020-11-09 19:38:46
mysql中Youcan’tspecifytargettableforupdateinFROMclau_MySQL
mysql中YoucantspecifytargettableforupdateinFROMclau_MySQL:mysql中You can't specify target table for update in FROM clause錯誤的意思是說,不能先select出同一表中的某些值,再update這個表(在同一語句中)。 例如下面這個sql: 代碼如下: delete from tbl where id in ( se
導讀mysql中YoucantspecifytargettableforupdateinFROMclau_MySQL:mysql中You can't specify target table for update in FROM clause錯誤的意思是說,不能先select出同一表中的某些值,再update這個表(在同一語句中)。 例如下面這個sql: 代碼如下: delete from tbl where id in ( se

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不會出現此問題。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
mysql中Youcan’tspecifytargettableforupdateinFROMclau_MySQL
mysql中YoucantspecifytargettableforupdateinFROMclau_MySQL:mysql中You can't specify target table for update in FROM clause錯誤的意思是說,不能先select出同一表中的某些值,再update這個表(在同一語句中)。 例如下面這個sql: 代碼如下: delete from tbl where id in ( se