将嵌套的sql update查询转换为JOIN操作

时间:2014-04-19 07:08:26

标签: sql oracle join nested

我想知道如何将查询更改为JOIN,结果相同:

update CDR 
set CDR_TYPE = 'ON_NET' 
where anum in (select subscriber 
               from DEGREE 
               where sub_type = 'MTN')
  and bnum in (select subscriber 
               from DEGREE 
               where sub_type = 'MTN')

仅供参考:我使用的是ORACLE数据库。

最诚挚的问候。

2 个答案:

答案 0 :(得分:1)

您可以使用WITH子句来删除重复的子查询。

WITH subquery AS (select subscriber 
               from DEGREE 
               where sub_type = 'MTN')
UPDATE cdr
SET cdr_type = 'ON_NET' 
WHERE anum IN (subquery)
  AND bnum IN (subquery);

答案 1 :(得分:0)

UPDATE 
(SELECT cdr_type 
 FROM cdr c
 INNER JOIN degree d
 ON c.anum = d.subscriber 
    AND c.bnum = d.subscriber
 WHERE d.sub_type = 'MTN'
) t
SET t.cdr_type = 'ON_NET'