berkist34 berkist34 - 3 months ago 18
MySQL Question

All customers having orders before 2 months ago and not after

I have an orders table with columns: userID and createdTime

I want to list my customers having reservation before 2 months ago and do not make reservations from 2 months ago to now.

I list all my customers having reservation before 2 months ago with this query

SELECT userId,createdTime,FROM_UNIXTIME(createdTime)
FROM
mytable
GROUP BY userId HAVING createdTime < 1466035200


However how can I filter the ones having reservations after that time?

Thanks

Answer
SELECT userId
FROM mytable
GROUP BY userId
HAVING sum(FROM_UNIXTIME(createdTime) < curdate() - interval 2 month) > 0
   AND sum(FROM_UNIXTIME(createdTime) > curdate() - interval 2 month) = 0