update table1 set sl = nvl((select sl from table2 where table1.bh=table2.bh),0)

解决方案 »

  1.   

    to lianhg(lianhg) :在table1中有三条记录('01',1),('02',1),('03',1)
    在table2中有两条记录('01',10),('02',10)
    我想更新之后,table1中的记录变为('01',10),('02',10),('03',0)
    因为table2中没有bh='03'的记录,所以table1中bh='03'的sl应该变成0。这应该怎么办呢?
      

  2.   


    SQL> drop table table1;SQL> drop table table2;SQL> create table table1 (bh char(2),sl  number(3));SQL> create table table2 (bh char(2),sl  number(3));SQL> insert into table1 values ('01',1);SQL> insert into table1 values ('02',1);SQL> insert into table1 values ('03',1);SQL> insert into table2 values ('01',10);SQL> insert into table2 values ('02',10);SQL> commit;
    SQL> select * from table1;BH         SL
    -- ----------
    01          1
    02          1
    03          1SQL> update table1 set sl = nvl((select sl from table2 where table1.bh=table2.bh
    ),0);SQL> select * from table1;BH         SL
    -- ----------
    01         10
    02         10
    03          0