Consulta SQL SELECT para dos valores dentro del mismo campo

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.

Gracias de antemano.

preguntado el 31 de julio de 12 a las 10:07

Can you add some sample data? -

'1' <> '2' for all values of '1'. -

@wildplasser What do you mean by '1' <> '2' for all values of '1'? Sorry new to this. -

@MattGibson sample data for all tables stated? -

AND e.discount_code_id = '1' AND e.discount_code_id != '2' . The second condition is redundant. -

1 Respuestas

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

Respondido 31 Jul 12, 13:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.