oracle update多表關聯

2021-03-31 14:40:40 字數 3289 閱讀 7098

update a.a3 = a.a3+b.b3 的問題

表a 結構 : a1 , a2 ,a3

表b 結構: b1,  b2,  b3

其中 a1 ,b1 為pk ,切值相同 就是可以使用a1 = b1 了.

請問用sql 語句或過程該如何實現如下的功能???

更新a 表的 a3 ,用a.a3 與b.b3之和更新.

3>update a

set a3 = (select a.a3 + b.b3 from b where a.a1 = b.b1) ;

7>update (select a1,a3,b1,b3 from a,b where a1=b1) set a3=a3+b3

開執行計畫, 談論效率是沒有太多的意義的^_^..

三樓的寫法與7樓的寫法得到的結果是不同的.

三樓的寫法會更新所有記錄, 而7樓的寫法只修改兩者相交的相關記錄資訊.

7樓的寫法可以更加容易的控制這條update語句的執行計畫, 不過要求b表必須在對應的字段上有主鍵索引:) , 在b表在對應欄位上有主鍵索引的時候, 建議使用7樓的寫法.

可以參考一下這個帖子^_^

(測試沒有成功..不知道怎麼搞的.)

參考了下面的:

非常佩服。抱著學習的態度,重寫了一下三樓的,在沒有主鍵的情況,請指教:

update a

set a3=(select a3+b3 from b where a1=b1)

where a1=(select b1 from b where a1=b1)

如下:

update con_eme_on20050309 a set a.con_price=(select a.con_price+(b.annuity-a.annuity)+(b.nojob-a.nojob)+(b.medicare-a.medicare)+(b.birthfee-a.birthfee)+(b.bruisefee-a.bruisefee) from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base),a.emp_base=(select b.emp_base from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base),a.annuity=(select b.annuity from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base),a.nojob=(select b.nojob from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base),a.medicare=(select b.medicare from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base),a.birthfee=(select b.birthfee from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base),a.bruisefee=(select b.bruisefee from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base),a.nojobbase=(select b.nojobbase from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base),a.mediabase=(select b.mediabase from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base),a.birthbase=(select b.birthbase from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base),a.bruisebase=(select b.bruisebase from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base)

where a.emp_cod in(select b.emp_cod from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base)

公積金:

update con_eme_on20050309 a set a.con_price=(select a.con_price+(b.accumulation-a.accumulation) from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base),a.accumulation=(select b.accumulation from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base),a.accumulationbase=(select b.accumulationbase from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base),a.accumulationbase1=(select b.accumulationbase1 from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base)

where a.emp_cod in(select b.emp_cod from con_eme_on200404 b where a.emp_cod=b.emp_cod and a.if_act='1' and a.emp_base!=b.emp_base)

oracle UPDATE 多表關聯更新

update customers a set city name select b.city name from tmp cust city b where b.customer id a.customer id where exists select 1 from tmp cust city b ...

hibernate criteria 多表關聯查詢

當qbc criteria關聯查詢的時候一般都需要取別名。今天就遇到乙個問題 先貼 修改之後正確的 override public searchresultfindlistpagebykeyword integer page,integer pagesize,string keyword,long ...

Mongoose aggregate 多表關聯查詢

使用mongoose操作mongodb資料庫進行關聯查詢是一種比較常見的操作,操作方式有哪幾種呢?下面用乙個具體的案例來演示。首先模擬一點資料,分別有 order 和 order item 兩個集合 order 集合裡的資料 order item 集合裡的資料 假設要做這樣的乙個操作 查詢 orde...