mysql 计算周末日期,如何计算MySQL中两个日期之间的星期六和星期日的总数

SELECT

floor(

datediff('2016-08-01','2016-07-01') / 7) * 2 + (

CASE WHEN

IF (weekday('2016-08-01') >= 5, 4,weekday('2016-08-01')) >=

IF (weekday('2016-07-02') >= 5, 4,weekday('2016-07-01'))

THEN

IF (weekday('2016-08-01') >= 5, 4,weekday('2016-08-01')) -

IF (weekday('2016-07-01') >= 5, 4,weekday('2016-07-01'))

ELSE

5 +

IF (weekday('2016-08-01') >= 5, 4,weekday('2016-08-01')) -

IF (weekday('2016-07-01') >= 5, 4, weekday('2016-07-01'))

END

) weekdays;

when i execute this code it always return 9 i don't know where i am doing mistake please suggest me where am doing wrong. while between Saturday and Sunday between given date should be 10 please suggest me

解决方案

If you don't have a table having all the dates between your start date and end date (inclusive) then you need to adopt a query which will create all the dates between your given date range (inclusive) first. Then use WEEKDAY function of MySQL to check whether the day is Saturday or Sunday

SELECT

dateTable.Day,

DAYNAME(dateTable.Day) AS dayName

FROM

( SELECT ADDDATE('2016-07-01', INTERVAL @i:=@i+1 DAY) AS DAY

FROM (

SELECT a.a

FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a

CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b

CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c

) a

JOIN (SELECT @i := -1) r1

WHERE

@i < DATEDIFF('2016-08-01', '2016-07-01')

) AS dateTable

WHERE WEEKDAY(dateTable.Day) IN (5,6)

ORDER BY dateTable.Day;

Note:

EDIT:

If you need only count:

SELECT

COUNT(*) AS total

FROM

( SELECT ADDDATE('2016-07-01', INTERVAL @i:=@i+1 DAY) AS DAY

FROM (

SELECT a.a

FROM (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS a

CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS b

CROSS JOIN (SELECT 0 AS a UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) AS c

) a

JOIN (SELECT @i := -1) r1

WHERE

@i < DATEDIFF('2016-08-01', '2016-07-01')

) AS dateTable

WHERE WEEKDAY(dateTable.Day) IN (5,6)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值