使用join在oracle中更新语句

时间:2014-11-04 07:45:08

标签: sql oracle oracle10g

我已经制作了这个sql,我需要用给定的结果更新表

SELECT DISTINCT t1.t1_val3, t1.t1_val4
                DECODE (b_val,
                        'A', 'Its A',
                        'B', 'Its B',
                        'C', 'Its C',
                        NULL
                       ) decode_val,

           FROM t1, t2, t3
          WHERE t1.t1_val = t2.t2_val
            AND t2.t2_val = t3.t3_val
            AND t3.t3_val2 <> 'PSA'
            AND t3.t3_val2 = 'Y'

现在使用此查询我需要更新t1表。

这样的事,

update t1 
set
t1.val5=decode_val
where t1.t1_val3= value returned from above query(first column t1_val3) 
and t1.t1_val4= value returned from above query(2nd column t1_val4)

DB - 10g

2 个答案:

答案 0 :(得分:2)

由于您使用的是10g,MATCHEDNOT MATCHED条款现在为 optional

MERGE INTO t1 a
USING (SELECT DISTINCT t1.t1_val3, t1.t1_val4
                DECODE (b_val,
                        'A', 'Its A',
                        'B', 'Its B',
                        'C', 'Its C',
                        NULL
                       ) decode_val,

           FROM t1, t2, t3
          WHERE t1.t1_val = t2.t2_val
            AND t2.t2_val = t3.t3_val
            AND t3.t3_val2 <> 'PSA'
            AND t3.t3_val2 = 'Y') b
ON(a.t1_val3 = b.t1_val_3 and a.t1_val4 = b.t1_val4)
WHEN MATCHED THEN 
   UPDATE SET a.t1.val5 = b.decode_val

答案 1 :(得分:1)

试试这个:

update t1 
set
t1.val5 =DECODE (b_val,
                    'A', 'Its A',
                    'B', 'Its B',
                    'C', 'Its C',
                    NULL
                   ) 

       FROM t1, t2, t3
      WHERE t1.t1_val = t2.t2_val
        AND t2.t2_val = t3.t3_val
        AND t3.t3_val2 <> 'PSA'
        AND t3.t3_val2 = 'Y'