简体   繁体   中英

SQL SELECT query for two values within same field

I am fairly new to SQL queries and I am having trouble performing a query on an already running database.

I need to run two queries, first to check who has signed up 3 days ago and has not made any orders and has not receieved an email giving them an offer. This works great.

The next query is to check that a customer has signed up 7 days ago, not made any orders and has receieved the 3 day email. I can get it to do that but I also need to check that they have not already receieved the second email and this is the part that is confusing me. Even when the customer has been sent the 2 email when I run the query again they still appear.

$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'))."'
";

The two discount codes are 1 for first email (5% discount) and 2 for the second email (10% discount) which is the value I am trying to check for that is within the database.

I am running the query in PHP. Any help would be grateful.

Thanks in advance.

Exclude "LEFT OUTER JOIN automated_email_discount" and conditions for this table from query and add following condition:

And c.customerID not in (select customerID from automated_email_discount where discount_code_id='2')

Here is the final statement:

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'))."'
";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM