I am trying to create a sequel query for emails of customers without accounts that have only placed orders on over 3 months. If i do the query
SELECT checkouts.email
FROM orders, checkouts
WHERE orders.id = checkouts.order_id AND orders.user_id is NULL
AND orders.created_at < CURDATE() - INTERVAL 3 MONTH
I get all emails from orders without accounts that were placed within the last 3 months, BUT it doesn't account for orders placed within the last 3 months without an account with the same emails from the sql query email list. Therefore, i want to use a MINUS operator to subtract all emails from recent orders. When i try this, i get an error:
SELECT checkouts.email
FROM orders, checkouts
WHERE orders.id = checkouts.order_id AND orders.user_id is NULL
AND orders.created_at < CURDATE() - INTERVAL 3 MONTH
MINUS
SELECT checkouts.email
FROM orders, checkouts
WHERE orders.id = checkouts.order_id AND orders.user_id is NULL
AND orders.created_at > CURDATE() - INTERVAL 3 MONTH
If i run this same query but with a UNION instead of a MINUS, it works, and essentially gives me all emails from orders without an account, regardless of the date.
Why does UNION work and not MINUS? How do i fix this query so i can get MINUS to work?