将值从一行替换为另一行

时间:2017-03-22 11:44:57

标签: sql oracle oracle11g

我收到了这段代码

UPDATE TASK tas1
SET TARGET_WORK_QTY = (SELECT REMAIN_WORK_QTY 
                       FROM TASK tas2
                       WHERE tas1.TASK_ID = tas2.TASK_ID
                         AND PROJ_ID = 884)

并且应该将值从一行重写到另一行(基于相同的task_id和project = 884),但它的工作方式有所不同。我正在使用oracle 11g。

示例表

  

id value1 value2
  1 aaa zzz
  2 bbb xxx
  3 ccc aaa

我想得到以下结果(将值从值1替换为值2):

  

id value1 value2
  1 aaa aaa
  2 bbb bbb
  3 ccc ccc

1 个答案:

答案 0 :(得分:0)

从您的示例数据和所需结果来看,您似乎只需要:

update task
set value2 = value1

例如:

SQL> create table task(id, value1, value2) as (
  2      select 1, 'aaa', 'zzz' from dual union all
  3      select 2, 'bbb', 'xxx' from dual union all
  4      select 3, 'ccc', 'aaa' from dual
  5  );

Table created.

SQL> update task set value2 = value1;

3 rows updated.

SQL> select * from task;

        ID VAL VAL
---------- --- ---
         1 aaa aaa
         2 bbb bbb
         3 ccc ccc

SQL>