日期:2014-05-17  浏览次数:20779 次

请问我如何把表中所有记录,用一个列的值去更改另外一个列的值
请问我如何把表中所有记录,用一个列的值去更改另外一个列的值

CREATE   TABLE   TEST
(
        ID                                                           NUMBER,
        A1                                                           VARCHAR2(10),
        A2                                                           VARCHAR2(10),
        A3                                                           VARCHAR2(10)
)

1.我想把TEST表中所有记录,把A2的值赋给A1(A1=A2),把A3的值赋给A2(A2=A3)
请问sql语句该怎么写,只用一个sql语句

下面是我写的sql   执行错误,不知道应该怎么写
update   TESTTB   tb1   set(tb1.a1   =   tb2.a2,tb1.a2   =   tb2.a3)
from
(select   tb2.a2,tb2.a3   from   TESTTB   tb2   where   tb1.ID   =   tb2.ID)   B,
  TESTTB   tb3
where  
  B.ID   =   tb3.ID

2.如果我想把所有记录的A1和A2的值互换又该怎么实现呢



------解决方案--------------------
update test set a1=a2,a2=a3;
------解决方案--------------------
上边的是1
2
update test set a1=a2,a2=a1;

------解决方案--------------------
update test t1 set (a1,a2)=(select a2,a3 from test t2 where t1.id=t2.id)
update test t1 set (a1,a2)=(select a2,a1 from test t2 where t1.id=t2.id)