SQL> select * from testc;F1         F2
-- ----------
1          10
2          20
3          30
4          40
5          50
6          60已选择6行。SQL> select * from testd;F1         F2
-- ----------
1         100
2         200
3         300
4         400
a         99以两个表中的F1为连接条件,将testc中的F2更新成testd中对应的F2,如果对应不上,则将值更新成a对应的F2,即99.用一条SQL语句如何实现。(尽量考虑性能)
(需要得到下面的结果:
SQL> select * from testc;F1         F2
-- ----------
1          100
2          200
3          300
4          400
5          99
6          99
)

解决方案 »

  1.   

    select testc.f1, 
    case when exists (select * from testd where testd.f1=testc.f1)
    then (select f2 from testd where testd.f1=testc.f1)
    else 99 end
    as f2
    from testc;
      

  2.   

    update testc c set f2 = nvl((select f2 from testd d where c.f1=d.f1),(select f2 from testd d where d.f1='a')
      

  3.   

    update testc c set f2 = nvl((select f2 from testd d where c.f1=d.f1),
                                (select f2 from testd d where d.f1='a'))
      

  4.   

    非常感谢onejune4450(中文字符)!
    同时谢谢 masterz(www.fruitfruit.com)和 liuyi8903(甜脆夹心) 和的热心!