sql 计算工作日数

一、创建表

CREATE TABLE non_working_days  (
  `date` date NOT NULL
)

2023-01-25
2023-04-02
2023-04-22
2023-08-20
2023-09-29

二、函数

CREATE FUNCTION calculate_workday(start_time DATE, end_time DATE)
    RETURNS INT
BEGIN
    DECLARE day_diff INT;

    SELECT   DATEDIFF(end_date, start_date) + 1 - COUNT(non_working_days.date) into day_diff
    FROM  non_working_days
              CROSS JOIN
          (SELECT start_time AS start_date,end_time AS end_date) AS date_range
    WHERE  non_working_days.date BETWEEN start_date AND end_date;

    RETURN day_diff;
END

三 测试

-- 测试
SELECT calculate_workday('2024-02-20 12:12:01', '2024-02-29')

四、 第三方获取假期接口

"https://timor.tech/api/holiday/year/"

你可能感兴趣的:(sql,mysql,数据库)