获取一个月中周六和周日的数量

bla*_*bee 5 mysql

我正在尝试获取由 end_date 和开始日期表示的给定月份的周六和周日的数量。

我一直试图在这里了解SO 问题中的算法。我已经明白,他们所创建的矩阵,但我不明白的是正在使用的算法,再加上事情是需要什么刚刚相反,这一点是相当新的给我,主要是我做正常的selectinsertupdatejoins在mysql。

在这个问题上获得一些想法会很有帮助。

Len*_*art 8

我会建议该线程中也讨论过的日历表。除了这个之外,它还有助于许多计算。然后您的查询可以表示为:

select count( case when DAYOFWEEK(calendar_date) in (1,7) then 1 end)
from Calendar 
where calendar_date between :start_date and :stop_date
Run Code Online (Sandbox Code Playgroud)

或作为:

select count(1)
from Calendar 
where calendar_date between :start_date and :stop_date
  and DAYOFWEEK(calendar_date) in (1,7)
Run Code Online (Sandbox Code Playgroud)