oracle9i环境下,客户端是xp环境,现有一表A,有字段mm,nn,现表中有数据如下:
           
        mm     nn        1      飞机
        5       汽车
        12      轮船
        13      汽车
        15     自行车 
现在想把mm=5的nn值由"汽车"改成"火箭",把mm=12的nn值由"轮船"改成"摩托车",把mm=15的nn值由"自行车"改成"汽车",改完后表中数据应该是:        mm     nn        1      飞机
        5       火箭
        12      摩托车
        13      汽车
        15     汽车要求只用update  set 语句,一次执行完成,不知道具体的sql语句该怎么写??? 

解决方案 »

  1.   

    update t a
    set nn=(select decode(mm,5,'火箭',12,'摩托',15,'汽车',nn) from t b
    where a.mm=b.mm)
      

  2.   

    update a set nn=decode(mm ,5,'火箭' , 12 ,'摩托车','汽车')
    where mm in (5,12,15)最好加上where条件,避免进行全表更新
      

  3.   


    SQL> create table t(idx int, cname varchar2(20));Table createdSQL> insert into t values(1,'a');1 row insertedSQL> insert into t values(2,'b');1 row insertedSQL> update t set cname=decode(idx,1,'aa',2,'bb');2 rows updatedSQL> select * from t;                                    IDX CNAME
    --------------------------------------- --------------------
                                          1 aa
                                          2 bbSQL> 
      

  4.   

    用decode函數來實現,這個函數相當SQL2000 case then 
      

  5.   

    CASE WHEN是标准SQL语法,基本上各种数据库都支持的
    DECODE是ORACLE专用的
    某些方面没有CASE使用方便,比如需要<或者>某个条件,或者多个条件并列的
      

  6.   

    update a set nn=(CASE 
    when mm = '5' then '火箭' 
    when mm = '12' then '摩托'  
    else '汽车'
    end )
    where mm in (5,12,15)