gpt4 book ai didi

mysql存储函数使用

转载 作者:行者123 更新时间:2023-11-30 23:41:33 25 4
gpt4 key购买 nike

我刚刚写了一个存储函数来计算两个日期之间的工作日。这行得通

select CountWeekDays('2010-03-07','2010-04-07')

这行不通

select CountWeekDays(o.order_date,o.created_date) from orders o;

知道如何让它发挥作用吗??

函数定义

delimiter $$;

CREATE FUNCTION CountWeekDays (sdate VARCHAR(50), edate VARCHAR(50)) RETURNS INT

BEGIN

DECLARE wdays, tdiff, counter, thisday smallint;

DECLARE newdate DATE;

SET newdate := sdate;

SET wdays = 0;

if DATEDIFF(edate, sdate) = 0 THEN RETURN 1; END IF;

if DATEDIFF(edate, sdate) < 0 THEN RETURN 0; END IF;

label1: LOOP

SET thisday = DAYOFWEEK(newdate);

IF thisday BETWEEN 2 AND 6 THEN SET wdays := wdays + 1; END IF;

SET newdate = DATE_ADD(newdate, INTERVAL 1 DAY);

IF DATEDIFF(edate, newdate) < 0 THEN LEAVE label1; END IF;

END LOOP label1;

RETURN wdays;

END

最佳答案

我不太了解功能,但如果它有效,请尝试,如果无效,请抱歉。

select CountWeekDays(o.order_date,o.created_date) from orders o where o.id =1;

关于mysql存储函数使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2584324/

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