UPDATE語句關聯時需注意的事

2021-10-02 07:57:39 字數 480 閱讀 6470

oracle庫中,關聯其他表進行更新時,使用如下語句會出現乙個問題。

update student a set a.name = (

select b.name from student_copy b where b.id = a.id

)

執行完會發現,a表中未與b表對應上的記錄,name字段值均變為null了。我們想要達到的效果是只有a表的id與b表的id能關聯時,才執行更新操作,解決此問題需要在後面加上where條件:

update student a set a.name = (

select b.name from student_copy b where b.id = a.id

)where exists ( select 1 from student b where b.id = a.id )

這樣就會保證只有a與b關聯上的記錄才會執行更新操作。

多表關聯的update語句

mssql的多表關聯的update語句 例如a表 存在一下字段 aid a1 a2 a3 a4 b表中存在字段 bid b1 b2 b3 b4 如果實現用b表的所有字段更新a表中相應的字段,在ms sql server裡面可以寫成 update a set a1 b.b1,a2 b.b2,a3 b....

多表關聯的update語句

mssql的多表關聯的update語句 例如a表 存在一下字段 aid a1 a2 a3 a4 b表中存在字段 bid b1 b2 b3 b4 如果實現用b表的所有字段更新a表中相應的字段,在ms sql server裡面可以寫成 update a set a1 b.b1,a2 b.b2,a3 b....

ORACLE 多表關聯 UPDATE 語句

oracle 多表關聯 update 語句 兩表 多表 關聯update 僅在 where 字句中的連線 直接賦值 update customers a 使用別名 set customer type 01 where exists select 1 from tmp cust city b wher...