简体   繁体   中英

MySQL select where the MOST RECENT date is NOT between two dates

I'm trying to run a query to retrieve customers whose most recent activity is not between +/- 30 days. For the life of me I can't figure it out.

Here's what I have so far:

Activity.date NOT BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) 
    AND DATE_ADD(CURDATE(), INTERVAL 30 DAY'

Problem is that I need to make sure that the Activity.created_on field is the MOST RECENT. I'm not sure how to go about this. I'm also joining it with the customers table based on customer_id, if that matters.

Thanks for the help

I've a similar table structure and this works for me:

select c.id, c.first_name, c.last_name,  max(a.activity_date) date from activities a join clients c on c.id = a.client_id
where a.activity_date
not between  DATE_SUB(CURDATE(), INTERVAL 30 DAY) and DATE_ADD(CURDATE(), INTERVAL 30 DAY)
group by c.id  
order by date desc, c.first_name, c,last_name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM