2014-10-30 42 views
0

我有两个针对oracle的查询。我需要修改它们的MySQL。 首先查询:更新:您无法在FROM子句中指定目标表'表'进行更新

UPDATE tec_onoff_file a 
SET emailtype = 'MIGR' 
WHERE EXISTS 
    (SELECT 1 
     FROM tec_onoff_file 
     WHERE emailtype = 'MIGR' 
     AND a.acctnbr = acctnbr 
     AND a.magabbr = magcodes); 

我改成了

update tec_onoff_file t1 
join tec_onoff_file t2 
    on t2.emailtype='MIGR' 
    and t1.acctnbr=t2.acctnbr 
    and t1.magabbr=t2.magcodes 
    set t1.emailtype='MIGR'; 

和它的作品。 但是第二个查询是很难对我来说

update tec_onoff_file a 
set emailtype = 'REIN' 
where transtype = 'REIN' 
and curracctnbr not in (select curracctnbr from tec_onoff_file b 
    where emailtype ='RENW' 
    and a.curracctnbr=b.curracctnbr); 

可能有人有帮助?我试图改变它像第一个查询JOIN,但它失败了,我不知道该怎么做。

+0

提供你的努力 – Slowcoder 2014-10-30 15:44:26

+0

对我来说,第二个查询逻辑看上去与'更新tec_onoff_file一 集EMAILTYPE ='REIN的不同 其中transtype =“REIN” 和(EMAILTYPE为NULL或EMAILTYPE查询!= 'RENW')'。 – Slowcoder 2014-10-30 15:53:53

+0

@Slowcoder也谢谢你,你的查询比较容易,并且与Strawberry的查询结果相同。现在我知道,我需要了解更多关于sql的知识。 – RexHunt 2014-10-31 09:22:50

回答

0
UPDATE tec_onoff_file a 
    LEFT 
    JOIN tec_onoff_file n 
    ON b.curracctnbr = a.curracctnbr 
    AND b.emailtype ='RENW' 
    SET emailtype = 'REIN' 
WHERE a.transtype = 'REIN' 
    AND b.transtype IS NULL; 
+1

解释不会伤害。 – Jubobs 2014-10-30 16:41:12

+0

@Strawberry谢谢,它的工作原理,但我不明白,为什么 AND b.transtype IS NULL; ? – RexHunt 2014-10-31 09:14:01

+0

这是简单的排除连接。 IS NULL语句只是返回表的第一个实例中的行,其第二个行中没有对应的行。 – Strawberry 2014-10-31 09:57:55

相关问题