Mysql更新结合内连接而不是excist

时间:2015-06-26 13:58:58

标签: mysql

经过一番努力之后,我终于有了一个工作代码来查询数据库并得到我想要的结果。

SELECT * 
FROM email_list_subscriber_events AS t1 INNER JOIN email_list_subscribers ON email_list_subscribers.subscriberid=t1.subscriberid
WHERE lastupdate BETWEEN 0 AND 9999999999 
      AND t1.listid = 206
      AND eventtype = 'Sent an Email Campaign'
      AND NOT EXISTS (SELECT 1
                      FROM email_list_subscriber_events AS t2
                      WHERE t1.subscriberid = t2.subscriberid 
                            AND t2.lastupdate BETWEEN 0 AND 9999999999 
                            AND t2.eventtype IN ('Opened an Email Campaign', 'Clicked on a link'))
GROUP BY `email_list_subscribers`.`subscriberid`

现在我正在努力将email_list_subscribers.confirmed从0更新为1或从1更新为0。

我尝试了几个UPDATE结构,但没有一个会解析。

感谢您的时间和精力。

更新

我无情的失败尝试

UPDATE epaper_list_subscribers
SET confirmed = 1
FROM email_list_subscriber_events AS t1 INNER JOIN email_list_subscribers ON email_list_subscribers.subscriberid=t1.subscriberid
WHERE lastupdate BETWEEN 0 AND 9999999999 
      AND t1.listid = 206
      AND eventtype = 'Sent an Email Campaign'
      AND NOT EXISTS (SELECT 1
                      FROM email_list_subscriber_events AS t2
                      WHERE t1.subscriberid = t2.subscriberid 
                            AND t2.lastupdate BETWEEN 0 AND 9999999999 
                            AND t2.eventtype IN ('Opened an Email Campaign', 'Clicked on a link'))

SELECT * 
FROM email_list_subscriber_events AS t1 INNER JOIN email_list_subscribers ON email_list_subscribers.subscriberid=t1.subscriberid
WHERE lastupdate BETWEEN 0 AND 9999999999 
      AND t1.listid = 206
      AND eventtype = 'Sent an Email Campaign'
      AND NOT EXISTS (UPDATE email_list_subscribers
                      SET email_list_subscribers.confirmed = 1
                      FROM email_list_subscriber_events AS t2
                      WHERE t1.subscriberid = t2.subscriberid 
                            AND t2.lastupdate BETWEEN 0 AND 9999999999 
                            AND t2.eventtype IN ('Opened an Email Campaign', 'Clicked on a link'))

由于在UPDATE中无法进行FROM,是否可以将select查询的结果输出到TEMP TABLE并对其进行更新?

SQLFIDDLE:http://sqlfiddle.com/#!9/0ee99

1 个答案:

答案 0 :(得分:0)

P.S:我还没有测试过这段代码。另外,验证列是否与正确的表相关联。

UPDATE epaper_list_subscribers
SET confirmed = 1
WHERE email_list_subscribers.subscriberid=epaper_list_subscribers.subscriberid 
      AND email_list_subscribers.lastupdate BETWEEN 0 AND 9999999999 
      AND email_list_subscriber_events.listid = 206
      AND email_list_subscribers.eventtype = 'Sent an Email Campaign'
      AND NOT EXISTS (SELECT 1
                      FROM email_list_subscriber_events AS t2
                      WHERE t1.subscriberid = t2.subscriberid 
                            AND t2.lastupdate BETWEEN 0 AND 9999999999 
                            AND t2.eventtype IN ('Opened an Email Campaign', 'Clicked on a link'))