錯誤的意思說,不能先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
)
再如:UPDATE bk_ygsbk AS bk SET REPORT_VISIT = ( SELECT COUNT(*) FROM bk_ygsbk WHERE ZYID = '1' ) WHERE bk.ZYID = '1'
應改成:UPDATE bk_ygsbk AS bk SET REPORT_VISIT = (SELECT * FROM ( SELECT COUNT(*) FROM bk_ygsbk WHERE ZYID = '1' ) AS r ) WHERE bk.ZYID = '1'
也就是說將select出的結果再通過中間表select一遍,這樣就規避了錯誤。注意,這個問題只出現於mysql,mssql和oracle不會出現此問題。