选择所有未使用MySQL和多个表打开电子邮件的订阅者

时间:2013-08-13 10:49:36

标签: mysql

我想选择属于某个用户的所有订阅者,并且过去一年中尚未已向其发送电子邮件。我的数据库结构如下:

Table: email_list_subscribers
Fields: subscriberid, listid, emailaddress

Table: email_lists
Fields: listid, name, ownerid

Table: email_list_subscriber_events
Fields: eventid, subscriberid, listid, eventownerid, eventdate, eventtype

所以这就是我对问题的措辞:

选择所有属于email_list的email_list_subscribers,其ownerid为2,并且在去年的email_list_subscriber_events中没有条目且eventtype等于“打开电子邮件广告系列”

因此,您可以看到这个非常复杂的查询,我无法理解。我已经成功选择了拥有正确所有者且年龄超过1年的所有订阅者,但无法合并他们尚未打开电子邮件。

有没有人有任何想法?

1 个答案:

答案 0 :(得分:0)

-- Select all email_list_subscribers
SELECT 
   es.*
FROM
   email_list_subscribers AS es
-- have an entry in email_list_subscriber_events that is 
   LEFT JOIN email_list_subscriber_events AS ee ON (
      ee.subscriberid = es.subscriberid
-- an eventtype equal to "Opened an Email Campaign"
      AND ee.eventtype = 'Opened an Email Campaign'
-- within the last year
      AND ee.eventdate > CURENT_DATE() - INTERVAL 1 YEAR 
      )
-- who belong to an email_list with an ownerid of 2
WHERE
   es.listid = 2
-- and who don't have an entry in email_list_subscriber_events ...
   AND email_list_subscriber_events.subscriberid IS NULL;