该月的第 N 个工作日

mis*_*tin 3 mysql

我想做的伪代码:

SELECT * FROM table WHERE NTH_DAY(DATE(table_date)) = NTH_DAY($input_date);
Run Code Online (Sandbox Code Playgroud)

我想确定给定日期该月的第 n 个工作日。例如,如果输入“2013-08-30”,则应返回 5,因为这是该月该工作日(星期五)第五次出现。

我读过无数类似的问题,但大多数都在寻找相反的问题,例如他们想找到第五个星期五的日期,而我想确定日期的第 n 个数字是多少。其他问题似乎是我所追求的,但它们采用的是我不理解的不同编程语言。

有人可以解释一下这在 MySQL 查询中是否可行以及如何实现吗?

Gor*_*off 5

要找到给定的“第 n”天是相当容易的:

select (case when day(table_date) between 1 and 7 then 1
             when day(table_date) between 8 and 14 then 2
             when day(table_date) between 15 and 21 then 3
             when day(table_date) between 22 and 28 then 4
             else 5
        end) as NthDay
Run Code Online (Sandbox Code Playgroud)

您还可以使用“整数”算术来完成此操作:

select 1 + floor((day(table_date) - 1) / 7) as NthDay
Run Code Online (Sandbox Code Playgroud)