原始数据:
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语句进行批量更新

解决方案 »

  1.   

    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'
      

  2.   

    这个准确点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'
      

  3.   


    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 selectedSQL> 
    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 updatedSQL> commit;
    Commit completeSQL> 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
    我更喜欢这种方法
      

  4.   

    update t0 t1 
    set 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%'