dousuize3082 2012-11-26 12:02
浏览 72
已采纳

从MySQL查询中排除特定假期

I have this query that I'm using to pull records from my DB:

SELECT dateAdded, claimedDate, TIMESTAMPDIFF(SECOND, dateAdded, claimedDate) AS output FROM leads 
WHERE HOUR(dateAdded) >= '9' 
AND HOUR(dateAdded) < '18' 
AND DAYOFWEEK(dateAdded) != 7 
AND DAYOFWEEK(dateAdded) != 1

As it stands, I'm currently only selecting records with a timestamp that falls between 9:00am - 5:00pm, Monday through Friday. I also need to be able to exclude the following major holidays: New Year's Day, Memorial Day, Independence Day, Labor Day, Thanksgiving, and Christmas.

I need those holidays excluded for all time. For example, the query should exclude all records that occur on the fourth Thursday of Nov (Thanksgiving), regardless of the year.

This doesn't necessarily need a MySQL-only solution. If it's easier to do a hybrid PHP/MySQL solution that would also work great. Thanks for the help.

  • 写回答

2条回答 默认 最新

  • dongqin1167 2012-11-26 12:04
    关注

    You need a table of dates that meet your "holiday" criteria, and then you JOIN to that table.

    Here's an example

    Otherwise, you'll need to simply exclude them...

    ... WHERE dateAdded NOT IN ('2012-07-04', ...)
    

    Update

    This solution uses a JOIN to omit holidays.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部