I have an ecommerce MySQL database that stores order information in the table ‘orders’
I need to select all NEW customers for the month of March. Customers do not have to register to order, so I’m working simply with the customer email and the date time of the order. So select all customer emails that don’t feature at all in previous months.
The database table has about 100k rows, and I’m looking for an efficient query.
I’m not too good with SQL (you can probably tell!), but this is what I have…
SELECT distinct(user_email) FROM orders
WHERE order_datetime > '2011-03-01 00:00:00'
AND order_datetime < '2011-03-31 23:59:59'
AND user_email NOT IN (
SELECT user_email FROM orders
WHERE order_datetime < '2011-03-01 00:00:00')
GROUP BY user_email
Is that a really convoluted way of doing things? 🙂 Please let me know if there’s a more speed efficient way…
Thanks, Rich
How about this:
HAVINGclause.order_datetimewill help.