如何将相关子查询与外表联系起来?

时间:2014-02-12 10:07:03

标签: mysql sql join updates correlated-subquery

我有这样的查询 -

UPDATE ACTION a 
INNER JOIN subscriberinfo s ON a.subscriberId = s.id AND a.subscriberId=118  
INNER JOIN ticket t ON t.subscriberId = s.id AND s.id=118 
SET a.exceedusage = (SELECT FORMAT(((SUM(dataVolumeDownLink + dataVolumeUpLink))/1048576),2) 
                    FROM cdr c 
                    WHERE c.msisdn =12424474969 
                    AND c.msisdn = s.msisdn
                    AND c.eventDate>t.cdrEventDate 
                    AND c.eventDate < a.actionTakenOn) 
WHERE a.remark='Reason : Data limit crossed' 
AND a.exceedusage IS NULL;

我想更新操作表列,我在这个查询中做错了吗?

如果可能,请向我解释一下如何执行此类更新的示例。

1 个答案:

答案 0 :(得分:0)

请使用汇总功能所在的组:

  UPDATE ACTION a 
    INNER JOIN subscriberinfo s ON a.subscriberId=118  
    INNER JOIN ticket t ON s.id=118 
    SET a.exceedusage = (SELECT FORMAT(((SUM(dataVolumeDownLink + dataVolumeUpLink))/1048576),2) 
                        FROM cdr c 
                        WHERE c.msisdn =12424474969 
                        AND c.eventDate>t.cdrEventDate 
                        AND c.eventDate < a.actionTakenOn group by c.msisdn ) 
    WHERE a.remark='Reason : Data limit crossed' 
    AND a.exceedusage IS NULL;