简体   繁体   中英

Delete rows where older than 5 Working days

I need to create an event. In this event I delete rows of records that are older than 5 WORKING days .

Notice the WORKING days ( Monday, Tuesday, Wednesday, Thursday, Friday )

I got this so far:

CREATE EVENT `delete_news` 
ON SCHEDULE EVERY 1 DAY 
STARTS '2014-03-01 00:00:00' 
ENDS '2014-03-31 00:00:00' 
ON COMPLETION 
NOT PRESERVE 
ENABLE 
DO 
DELETE FROM news WHERE time_created < (NOW() - INTERVAL 5 DAY)

How can I determine the WORKING days in mysql?

Edit:

DELETE FROM news 
WHERE 
    time_created < ( NOW( ) - INTERVAL (
        CASE WEEKDAY(time_created) 
            WHEN 4 THEN 7 
            WHEN 5 THEN 6 
            ELSE 5 
        END) 
     DAY )

Use WEEKDAY() or DAYOFWEEK() MySQL functions (depending on your country). Depending on day number subtract 5 or 6 or 7 days

time_created < (NOW() - INTERVAL if(WEEKDAY()=..., 5, else if 6, else if 7) DAY)

you should try this not an exactly:

DELETE FROM jmail_history
WHERE date < DATE_SUB(NOW(), INTERVAL 5 DAY)
AND date != 
(
    SELECT MAX(date) FROM jmail_history
    WHERE date < DATE_SUB(NOW(), INTERVAL 5 DAY)
)

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