删除超过5个工作日的行

时间:2014-03-20 08:02:35

标签: mysql sql

我需要创建一个活动。在这种情况下,我会删除早于 5个工作日的记录行。

注意工作日(周一,周二,周三,周四,周五)

到目前为止我得到了这个:

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)

如何确定mysql中的WORKING天?

修改

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

2 个答案:

答案 0 :(得分:2)

使用WEEKDAY()DAYOFWEEK() MySQL功能(取决于您所在的国家/地区)。根据日期数减去5或6或7天

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

答案 1 :(得分:0)

您应该尝试这不是:

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)
)