有两张表表一:SAVEBAL                  表二:CROPSUB
ACCNO           SUBNO         ACCNO           CNTYPE
668503759       212341        668503759           1
192189521       235111        192189521           1
192325079       123672        192371630           1
192371630       235444        279293591           1
279293591       123777        279293591           1
279349348       198121        279349348           1
279394683       235081        279394683           1
      表一和表二的ACCNO是关联的.
我现在想把表一里SUBNO的值以  "235"开头的 ACCNO对应在表二里的CNTYPE值 由1 更新为2即写一个 update语句 使表二变成:表二:CROPSUB
ACCNO           CNTYPE
668503759           1
192189521           2
192371630           1
279293591           2
279293591           1
279349348           1
279394683           2
请高手帮忙写一个update语句,谢谢.

解决方案 »

  1.   

    update CROPSUB a set CNTYPE = 2 where exists (select 1 from SAVEBAL b where b.ACCNO = a.ACCNO and to_char(b.SUBNO) like '235%');
      

  2.   


          
    create table savebal(accno int,sumno int);
    insert into savebal select 668503759,      212341 from dual;
    insert into savebal select 192189521,      235111 from dual;
    insert into savebal select 192325079,      123672 from dual;
    insert into savebal select 192371630,      235444 from dual;
    insert into savebal select 279293591,      123777 from dual;
    insert into savebal select 279349348,      198121 from dual;
    insert into savebal select 279394683,      235081 from dual;create table cropsub(accno int,cntype int);
    insert into cropsub select 668503759,      1 from dual;
    insert into cropsub select 192189521,      1 from dual;
    insert into cropsub select 192371630,      1 from dual;
    insert into cropsub select 279293591,      1 from dual;
    insert into cropsub select 279293591,      1 from dual;
    insert into cropsub select 279349348,      1 from dual;
    insert into cropsub select 279394683,      1 from dual;update cropsub a set a.cntype=2
    where exists(select 1 from savebal b where a.accno=b.accno and substr(b.sumno,1,3)=235);select * from cropsub;
      

  3.   


    create table savebal(accno int,sumno int);
    insert into savebal select 668503759,      212341 from dual;
    insert into savebal select 192189521,      235111 from dual;
    insert into savebal select 192325079,      123672 from dual;
    insert into savebal select 192371630,      235444 from dual;
    insert into savebal select 279293591,      123777 from dual;
    insert into savebal select 279349348,      198121 from dual;
    insert into savebal select 279394683,      235081 from dual;create table cropsub(accno int,cntype int);
    insert into cropsub select 668503759,      1 from dual;
    insert into cropsub select 192189521,      1 from dual;
    insert into cropsub select 192371630,      1 from dual;
    insert into cropsub select 279293591,      1 from dual;
    insert into cropsub select 279293591,      1 from dual;
    insert into cropsub select 279349348,      1 from dual;
    insert into cropsub select 279394683,      1 from dual;
    merge into cropsub a
    using savebal b
    on (a.accno=b.accno and substr(b.sumno,1,3)=235)
    when matched then update set a.cntype=2
    select * from cropsub;
    -----------
    668503759 1
    192189521 2
    192371630 2
    279293591 1
    279293591 1
    279349348 1
    279394683 2
    -------------
      

  4.   

    干净利落
    to_char(b.subno) like '235%'可以改成substr(b.subno,1,3)='235'
    若SUBNO字段是字符型的话,写成b.subno>='235' and b.subno<'236'
    效率应该更高
    楼主熟悉下exists的用法,其实不难的
      

  5.   


    exists  恩   学习了  我都忘了有这关键字了 
      

  6.   

    update cropsub set cntype=2 where accno in (select accno from savebal where subno like "235%")
    这么写对吗?