SELECT users.email FROM users
INNER JOIN orders
ON orders.user_id = users.id
WHERE orders.status = 'new'
AND orders.date_created > three months back?
I would like to grab all users email that have NOT made an order on my site within 3 months from today. So if no order within 3 months, grab the email.
date_created is type TIMESTAMP. So an example is: 2012-11-10 23:54:20
Also I would like an query for grabbing users email who have not made any orders at all (no rows in orders table)?
I tried with:
SELECT users.email FROM users
WHERE users.id NOT IN (select user_id from orders where user_id = users.id and orders.status = 'new')
But it seems to grab users with orders anyway?
Group the orders by user and filter the results for those where the most recent order is more than 3 months ago:
Make an outer join and filter for records that fall outside of the intersection: