0

我想选择属于某个用户并且在过去一年中没有收到电子邮件的所有订阅者。我的数据库结构如下:

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 且 ownerid 为 2 且在 email_list_subscriber_events 中没有最近一年内的条目且 eventtype 等于“Opened an Email Campaign”的所有 email_list_subscribers

因此,您可以看到这是一个相当复杂的查询,我无法理解。我已经设法选择了拥有正确所有者且超过 1 年的所有订阅者,但无法合并他们没有打开电子邮件的情况。

有没有人有任何想法?

4

1 回答 1

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;
于 2013-08-13T10:57:07.760 回答