我对SQL查询相当新,我在已经运行的数据库上执行查询时遇到问题。
我需要运行两个查询,首先检查谁在3天前注册过,并且没有发出任何订单,也没有收到电子邮件给他们报价。这非常有效。
下一个查询是检查客户是否在7天前注册,没有订单并收到3天的电子邮件。我可以做到这一点,但我还需要检查他们还没有收到第二封电子邮件,这是令我困惑的部分。即使客户在我再次运行查询时发送了2封电子邮件,它们仍会显示。
$sql = "
SELECT c.customerID, c.email, c.forename, c.date
FROM customers c
LEFT OUTER JOIN orders_headers o ON o.customerID=c.customerID
LEFT OUTER JOIN no_order_mail_sent m ON m.customerID=c.customerID
LEFT OUTER JOIN automated_email_discount e ON e.customerID=c.customerID
WHERE m.customerID IS NULL
AND o.customerID IS NULL
AND e.customerID IS NOT NULL
AND e.discount_code_id='1'
AND e.discount_code_id!='2'
AND STR_TO_DATE(c.date, '%Y%m%d') ='".date('Y-m-d', strtotime('-'.$days.'days'))."'
";
第一封电子邮件(5%折扣)的两个折扣代码为1,第二封电子邮件的折扣代码为2(10%折扣),这是我试图检查的数据库中的值。
我在PHP中运行查询。任何帮助都会很感激。
提前致谢。
答案 0 :(得分:0)
从查询中排除“LEFT OUTER JOIN automated_email_discount”和此表的条件并添加以下条件:
And c.customerID not in (select customerID from automated_email_discount where discount_code_id='2')
以下是最后声明:
SELECT c.customerID, c.email, c.forename, c.date
FROM customers c
LEFT OUTER JOIN orders_headers o ON o.customerID=c.customerID
LEFT OUTER JOIN no_order_mail_sent m ON m.customerID=c.customerID
WHERE m.customerID IS NULL
AND o.customerID IS NULL
And c.customerID not in (select customerID from automated_email_discount where discount_code_id='2')
AND STR_TO_DATE(c.date, '%Y%m%d') ='".date('Y-m-d', strtotime('-'.$days.'days'))."'
";