gpt4 book ai didi

MySQL 函数返回周末

转载 作者:行者123 更新时间:2023-11-29 19:20:16 25 4
gpt4 key购买 nike

我一直在尝试获取两个给定日期之间的周末天数(周六和周日)。

我遇到了这个解决方案:http://crossedlogic.blogspot.ca/2008/09/using-sql-to-find-work-days-in-date.html

当我调用该函数时,它在输出中返回 OK 作为消息,我希望它返回周末天数。

我做错了什么?

代码:

DROP FUNCTION IF EXISTS `fn_GET_WEEKEND_DAYS`;

DELIMITER $$
CREATE FUNCTION `fn_GET_WEEKEND_DAYS`(StartDate DATE, EndDate DATE) RETURNS INT
BEGIN
# declare the variables.
DECLARE varDays INT;

# create the temprorary table to insert the data in.
CREATE TEMPORARY TABLE temp(calendarDate DATE, isWeekend TINYINT(1));

# insert the starting date.
INSERT INTO temp VALUES(StartDate, NULL);

# insert each day by increment of 1 day untill reached the end date.
WHILE (SELECT MAX(CalendarDate) FROM temp) < EndDate DO
INSERT INTO temp
SELECT ADDDATE(MAX(CalendarDate), INTERVAL 1 DAY), NULL
FROM temp;
END WHILE;

# update the is weekend field depending if the day of week of each row is 1 or 7. (saturday or sunday)
UPDATE temp SET isWeekend = CASE WHEN DAYOFWEEK(calendarDate) IN (1, 7) THEN true ELSE false END;

# count the date that are weekends.
SELECT COUNT(calendarDate) INTO varDays FROM temp WHERE isWeekend = true;

# drop the temp table.
DROP TEMPORARY TABLE IF EXISTS temp;

RETURN varDays;
END $$

测试:

SELECT fn_GET_WEEKEND_DAYS(CURDATE(), ADDDATE(CURDATE(), INTERVAL 10 DAY)) AS TEST;

任何帮助和建议将不胜感激。

最佳答案

快速修复当前解决方案:-

DROP FUNCTION IF EXISTS `fn_GET_WEEKEND_DAYS`;

DELIMITER $$
CREATE FUNCTION `fn_GET_WEEKEND_DAYS`(StartDate DATE, EndDate DATE) RETURNS INT
BEGIN
# declare the variables.
DECLARE varDays INT;
DECLARE varDate DATE;

IF (StartDate > EndDate) THEN
SET varDate = StartDate;
SET StartDate = EndDate;
SET EndDate = varDate;
END IF;

# create the temprorary table to insert the data in.
CREATE TEMPORARY TABLE temp1(calendarDate DATE, isWeekend TINYINT(1));

# insert the starting date.
INSERT INTO temp1 VALUES(StartDate, NULL);

# insert each day by increment of 1 day untill reached the end date.
SET varDate = StartDate;
WHILE varDate < EndDate DO
INSERT INTO temp1 VALUES(ADDDATE(varDate, INTERVAL 1 DAY), NULL);
SELECT MAX(CalendarDate) INTO varDate FROM temp1;
END WHILE;

# update the is weekend field depending if the day of week of each row is 0 or 1. (saturday or sunday)
UPDATE temp1 SET isWeekend = CASE WHEN DAYOFWEEK(calendarDate) = 7 OR DAYOFWEEK(calendarDate) = 1 THEN true ELSE false END;

# count the date that are weekends.
SELECT COUNT(calendarDate) INTO varDays FROM temp1 WHERE isWeekend = true;

# drop the temp1 table.
DROP TEMPORARY TABLE IF EXISTS temp1;

RETURN varDays;
END $$

大问题是它正在使用临时表,并尝试根据临时表中的选择插入临时表,但这是无法完成的。它将给出错误您不能在同一查询中多次引用临时表。例如,以下内容不起作用:

如果日期范围有限,那么在单个 SQL 语句中可能很容易完成

编辑

一种使用单个 SQL 语句执行此操作的方法,复制最大总日期范围为 9999 天:-

SELECT SUM(IF(DAYOFWEEK(DATE_ADD('2014-05-02', INTERVAL units.aCnt + tens.aCnt * 10 + hundreds.aCnt * 100 + thousands.aCnt * 1000 DAY) ) IN (1,7), 1, 0))
FROM (SELECT 1 AS aCnt UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) units
CROSS JOIN (SELECT 1 AS aCnt UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) tens
CROSS JOIN (SELECT 1 AS aCnt UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) hundreds
CROSS JOIN (SELECT 1 AS aCnt UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) thousands
WHERE '2017-03-01' >= DATE_ADD('2014-05-02', INTERVAL units.aCnt + tens.aCnt * 10 + hundreds.aCnt * 100 + thousands.aCnt * 1000 DAY)

关于MySQL 函数返回周末,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42490529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com