tags:

views:

68

answers:

2

I have a query that gets a list of emails who have subscribed for a newsletter trial which lasts 30 days..

$thirty = time() - 3024000;

SELECT c.email 
  FROM tbl_clients AS c 
  JOIN tbl_clientoptions AS o ON o.client = c.id 
 WHERE o.option = 'newsletter' 
   AND c.datecreated > $thirty

What I want to do is do a check in that same query so it also returns clients OVER 30 days old if they have the tbl_clientoptions.option = 'trialoverride' (ie; a row in the client options table with the value "trialoverride")

TBL_CLIENTS table:

  • id
  • name
  • email
  • datecreated

TBL_CLIENTOPTIONS table:

  • id
  • client
  • option
A: 

How about:

SELECT c.email 
   FROM tbl_clients AS c JOIN tbl_clientoptions AS o
   ON o.client = c.id 
   WHERE (o.option = 'newsletter' AND c.datecreated > $thirty) 
     || o.option = 'trialoverride';
catfarm
I think I tried this, but it conflicts with the other 'option' column (ie: the row cant equal 'newsletter' and 'trialoverride' at the same time) Is this right?
Sam
Sorry, didn't see that. I've edited it to what I think should work instead. Basically, moved the ()s around a little.
catfarm
A: 

Normally just a OR condition will do it, added a DISTINCT if a same user user can have both condition, otherwise remove it :

SELECT DISTINCT c.email 
FROM tbl_clients c 
 INNER JOIN tbl_clientoptions o ON (o.client = c.id) 
WHERE
 (o.option = 'newsletter' AND c.datecreated < $thirty) OR
 (o.option = 'trial override' AND c.datecreated > $thirty);
Patrick