如何使用mysql获取工作日数

时间:2015-04-16 09:59:36

标签: mysql sql

我必须找到使用mysql的工作日数。我正在使用此查询,但这并没有给我正确的结果

 SELECT ((DATEDIFF('2015-05-31', '2015-05-01')) -((WEEK('2015-05-31') - WEEK('2015-05-01')) * 2) - 
(case when weekday('2015-05-31') = 6 then 1 else 0 end) - (case when weekday('2015-05-01') = 5 then 1 else 0 end))
as DifD ;

作为输出给出19,营业日数应为20 有人请帮忙

1 个答案:

答案 0 :(得分:1)

试试这个!

SET @i=-1;
SELECT SUM(CASE WHEN(WEEKDAY(ADDDATE('2015-05-01', INTERVAL @i:=@i+1 DAY))) < 5 THEN 1 ELSE 0 END) AS `business_days`
FROM `table`
WHERE @i < DATEDIFF('2015-05-31', '2015-05-01');

希望这个答案有所帮助!