我config表其中有個字段叫instrument,是instrument表的外鍵。
現(xiàn)在 config表中有臟數(shù)據(jù),也就是說 config表中有一系列數(shù)據(jù) 但是 通過config.instrument 在instrument表中查不到相關數(shù)據(jù),所以需要刪除這些多余的數(shù)據(jù)
我先寫了這個sql語句
delete from config where uid in
(select c.uid from config as c where c.instrument not in
(select uid from instrument))
但是出錯了

image.png
經(jīng)過查詢結果后得知,不能先select出同一表中的某些值,再update這個表(在同一語句中)
所以需要再嵌套一次查詢
delete from config where uid in
(select a.uid from
(select * from config as c where c.instrument not in
(select uid from instrument)) as a)
sql正常運行
注意的是,參考文檔說 這個問題只出現(xiàn)于mysql,mssql和oracle不會出現(xiàn)此問題。
ref>http://blog.csdn.net/priestmoon/article/details/8016121