我有这个vb.NET代码,想在MySQL中编写一个功能相同的函数。
Public Function IsWeekdayTimeInRange(DateToCheck As DateTime, StartDayOfWeek As DayOfWeek,
EndDayOfWeek As DayOfWeek) As Boolean
Dim WeekdaysInRange As New List(Of DayOfWeek)
WeekdaysInRange.Add(StartDayOfWeek)
Dim i As Integer = StartDayOfWeek
While i <> EndDayOfWeek
i += 1
i = If(i = 7, 0, i)
WeekdaysInRange.Add(i)
End While
Return WeekdaysInRange.Contains(DateToCheck.DayOfWeek)
End Function
目前在MySQL中,但是由于我从未编写过SQL函数,因此我需要一些帮助。
DELIMITER |
CREATE FUNCTION IS_BETWEEN_WEEKDAYS(d DATETIME, s INT, e INT)
RETURNS BOOLEAN DETERMINISTIC
BEGIN
DECLARE weekdayToCheck INT;
DECLARE result BOOLEAN;
DECLARE i INT;
SET result = false;
SET weekdayToCheck = WEEKDAY(d);
SET i = s;
WHILE(i <> e) DO
/* more logic here */
END WHILE;
RETURN result;
END; |
DELIMITER ;
编辑:自从我现在知道MySQL具有Weekday(Monday) = 0
以来,我的函数还需要转换为Weekday(Sunday) = 0
,因为我的数据使用该格式。
答案 0 :(得分:1)
不知道您对“之间”的定义是什么,但是在这里我使用了大于和小于
CREATE FUNCTION IS_BETWEEN_WEEKDAYS(d DATETIME, s INT, e INT)
RETURNS BOOLEAN DETERMINISTIC
BEGIN
DECLARE weekdayToCheck INT;
DECLARE result INT;
SET weekdayToCheck = WEEKDAY(d);
SET result = 0;
IF (s < e) THEN
IF (weekdayToCheck > s AND weekdayToCheck < e) THEN SET result = 1;
END IF;
END IF;
IF (s > e) THEN
IF (weekdayToCheck > s OR weekdayToCheck < e) THEN SET result = 1;
END IF;
END IF;
RETURN result;
END;
也许所有的if / else子句都可以缩短一些。
答案 1 :(得分:1)
考虑一个星期从星期一开始。希望这会有所帮助:
DELIMITER |
CREATE FUNCTION IS_BETWEEN_WEEKDAYS(d DATETIME, s INT, e INT)
RETURNS BOOLEAN DETERMINISTIC
BEGIN
DECLARE weekdayToCheck INT;
DECLARE result TINYINT(1);
SET weekdayToCheck = WEEKDAY(d);
SET result = 0;
IF(weekdayToCheck > s AND weekdayToCheck < e) THEN
SET result = 1;
ELSEIF (s > e AND weekdayToCheck < s AND weekdayToCheck < e) THEN
SET result = 1;
END IF;
RETURN result;
END; |
DELIMITER ;
答案 2 :(得分:0)
现在可以使用。它可能更简洁,但它帮助我进行了思考。
DELIMITER |
CREATE FUNCTION IS_BETWEEN_WEEKDAYS(d DATETIME, s INT, e INT, FirstDayOfWeekIsSunday BOOLEAN)
RETURNS BOOLEAN DETERMINISTIC
BEGIN
DECLARE weekdayToCheck INT;
DECLARE result BOOLEAN;
DECLARE i INT;
SET result = false;
SET weekdayToCheck = WEEKDAY(d);
SET i = s;
IF FirstDayOfWeekIsSunday = TRUE THEN
SET weekdayToCheck = weekdayToCheck + 1;
IF weekdayToCheck = 7 THEN
SET weekdayToCheck = 0;
END IF;
END IF;
IF weekdayToCheck = s THEN
SET result = true;
END IF;
WHILE(i <> e) DO
SET i = i + 1;
IF i = 7 THEN
SET i = 0;
END IF;
IF weekdayToCheck = i THEN
SET result = true;
END IF;
END WHILE;
RETURN result;
END; |
DELIMITER ;
编辑:修复了循环中订单的问题。还添加了用于处理s
和e
的功能的选项,前提是它们假设星期从星期日开始。
编辑:测试以上,它可以工作。还创建一个函数,该函数可以检查例如DateTime是否在星期二下午3点到星期四凌晨2点之间。
DELIMITER |
CREATE FUNCTION IS_BETWEEN_WEEKDAYS_AND_TIME(d DATETIME, s INT, starttime TIME, e INT, endtime TIME, FirstDayOfWeekIsSunday BOOLEAN)
RETURNS BOOLEAN DETERMINISTIC
BEGIN
DECLARE weekdayToCheck INT;
DECLARE result BOOLEAN;
DECLARE i INT;
DECLARE dTime TIME;
SET result = false;
SET weekdayToCheck = WEEKDAY(d);
SET i = s;
SET dTime = DATE_FORMAT(d, '%H:%i:%s');
IF FirstDayOfWeekIsSunday = TRUE THEN
SET weekdayToCheck = weekdayToCheck + 1;
IF weekdayToCheck = 7 THEN
SET weekdayToCheck = 0;
END IF;
END IF;
IF weekdayToCheck = s THEN
IF ((weekdayToCheck = s AND dTime > starttime) OR (weekdayToCheck = e AND dTime < endtime) OR (weekdayToCheck <> s AND weekdayToCheck <> e)) THEN
SET result = true;
END IF;
END IF;
WHILE(i <> e) DO
SET i = i + 1;
IF i = 7 THEN
SET i = 0;
END IF;
IF weekdayToCheck = i THEN
IF ((weekdayToCheck = s AND dTime > starttime) OR (weekdayToCheck = e AND dTime < endtime) OR (weekdayToCheck <> s AND weekdayToCheck <> e)) THEN
SET result = true;
END IF;
END IF;
END WHILE;
RETURN result;
END; |
DELIMITER ;