2014-11-04 73 views
0

我做這個SQL,我現在需要使用此查詢我需要更新T1表給出結果更新語句加入

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' 

更新表。

這樣的事情,

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 - 10克

回答

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 
+0

感謝拉利特。作品像一個魅力:) – 2014-11-04 09:19:50

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' 
+0

請提交之前驗證您的文章,始終使用代碼標籤,並檢查標籤是否正確應用。 – 2014-11-04 08:05:36

+0

im new here.sorry :) – 2014-11-04 08:07:38

+0

沒問題。我編輯了你的答案。 – 2014-11-04 08:36:19