原始資料:
id para acode fina
1111 2222 X1
1112 3333 X2
1113 4444 X3
1114 2222 X4
1115 2222 X5
2222 1234 1245 4578
3333 0000 12354 3695
3333 0000 12354 3695
1116 4251 4453 44524
更新邏輯:
1.若acode以X開頭,則把該行的para作為id查找原表
2.將1查找出來的fina去更新原表
更改后的資料
id para acode fina
1111 2222 X1 4578
1112 3333 X2 3695
1113 4444 X3 8527
1114 2222 X4 4578
1115 2222 X5 4578
2222 1234 1245 4578
3333 0000 12354 3695
4444 1235 3698 8527
1116 4251 4453 44524
求大神看看,用一個update陳述句進行批量更新
uj5u.com熱心網友回復:
update table a set fina = (select b.fina from table b where a.para=b.id)
where fina is null and substr(acode,0,1)='X'
uj5u.com熱心網友回復:
就參考一次嗎?uj5u.com熱心網友回復:
這個準確點update table a set fina = (select b.fina from table b where a.para=b.id and substr(acode,0,1)<>'X' and fina is not null)
where fina is null and substr(acode,0,1)='X'
uj5u.com熱心網友回復:
SQL> select * from t4;
ID PARA ACODE FINA
---------- ---- ----- -----
1111 2222 X1
1112 3333 X2
1113 4444 X3
1114 2222 X4
1115 2222 X5
2222 1234 1245 4578
3333 0000 12354 3695
4444 1235 3698 8527
1116 4251 4453 44524
9 rows selected
SQL>
SQL> update (
2 select /*+BYPASS_UJVC*/ b.fina finab ,a.fina finaa from t4 b,
3 (select para ,fina from t4 where substr(acode,1,1)='X') a
4 where a.para=b.id)
5 set finaa=finab
6 ;
5 rows updated
SQL> commit;
Commit complete
SQL> select * from t4;
ID PARA ACODE FINA
---------- ---- ----- -----
1111 2222 X1 4578
1112 3333 X2 3695
1113 4444 X3 8527
1114 2222 X4 4578
1115 2222 X5 4578
2222 1234 1245 4578
3333 0000 12354 3695
4444 1235 3698 8527
1116 4251 4453 44524
9 rows selected
我更喜歡這種方法
uj5u.com熱心網友回復:
4樓那個是在db2驗證的嗎?uj5u.com熱心網友回復:
另外一種寫法而已。茴香豆的茴有幾種寫法
uj5u.com熱心網友回復:
update t0 t1set fina = (
select t2.fina from t0 t2 where t2.id = t1.para and t1.acode like 'X%' and t1.fina is null
)
where fina is null and acode like 'X%'
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/110540.html
標籤:開發
上一篇:求oracel11g下載地址鏈接
下一篇:oracle查詢結果框有問題
