有一个声明并从另一个表中读取日期

时间:2016-03-01 14:07:39

标签: mysql unions

我在以下查询中遇到了一些问题:

SELECT DATE_FORMAT(date, "%Y-%m-%d") date, id, c, FromWhere FROM (
SELECT distinct cl.creationtime date, c.id, 1 c, "Created" as FromWhere -- Current amount of contacts in list
FROM `contactsLists` cl, `contacts` c
WHERE cl.contact=c.id AND c.optin="yes" AND c.restriction=0 AND cl.list = 13
AND cl.creationtime > DATE_SUB(now(), interval 13 MONTH)
UNION
SELECT IFNULL(cl.edittime, NOW()) date, c.id, -1 c, "Deleted from list" as FromWhere -- Deleted from list
FROM `contactsLists` cl, `contacts` c
WHERE cl.contact=c.id AND c.optin="yes" AND c.restriction=0 AND cl.deleted=1 AND cl.list = 13
AND IFNULL(cl.edittime, NOW()) > DATE_SUB(now(), interval 13 MONTH) 
UNION
SELECT o.optintime date, c.id, 1 c, "Optin added" as FromWhere -- Optins
FROM `contactsLists` cl, `contacts` c, `optin` o
WHERE cl.contact=c.id AND c.email = o.email AND o.reply="yes" AND c.restriction=0 AND cl.list = 13
AND o.optintime > DATE_SUB(now(), interval 13 MONTH)
UNION
SELECT o.optintime date, c.id, -1 c, "Optout added" as FromWhere -- Optouts
FROM `contactsLists` cl, `contacts` c, `optin` o
WHERE cl.contact=c.id AND c.email = o.email AND o.reply="no" AND c.restriction=0 AND cl.list = 13
AND o.optintime > DATE_SUB(now(), interval 13 MONTH)
ORDER BY date) u;

问题是最后2个工会“Optins and Optouts”正在读取optin状态o.reply =“no”/“yes”。但是它将情况的陈述读作now()。应该阅读optin回复,同时还要从o.optintime表中同时检查日期。这样可能吗?

除此之外,其他所有内容都在查询中。该图的想法是显示客户端数据库列表的增长或丢失。

0 个答案:

没有答案