首页 > 解决方案 > 我如何通过查询来适应 MYSQL 中每个月的最后一个业务/工作日?

问题描述

全部,

试图调整下面的查询来获取这个月的最后一个工作日,比如 5 月、6 月或其他什么?

select distinct
month(createddate),
year(CreatedDate),
id,
value1,
vaue2,
createddate,
count( distinct id)
from 
table

and value2 IN ('harry','sally')

AND createddate > LAST_DAY( '2020-05-21') ## i need last business day here for may. 
AND createddate < '2020-06-01' ##  i need first day of next month here
group by month(createddate),
year(CreatedDate), id,value1, value2,createddate

我有一段时间没有使用 mysql,有没有办法可以使用函数或存储过程来找出这个问题?如果是这样

***工作日是指工作日,不是周末***

标签: mysqlsql

解决方案


大概,你想避免周末。所以:

(createddate > last_day('2020-05-21') and dayofweek(last_day('2020-05-21') between 2 and 6 or
 createddate > last_day('2020-05-21') - interval 1 day and dayofweek(last_day('2020-05-21') = 7 or
 createddate > last_day('2020-05-21') - interval 2 day and dayofweek(last_day('2020-05-21') = 1
)

推荐阅读