努力查看我的方法的错误(mysql函数)

发布于 2024-10-12 13:26:14 字数 1466 浏览 4 评论 0原文

(请参阅下面我的答案以获取解决方案 - 感谢您的反馈)

这可能是非常明显的事情,但我看不出我的 sql 有什么问题:

mysql> CREATE FUNCTION start_of_minute(
->     curdate DATE)
->   RETURNS DATE
->   DETERMINISTIC
->   SQL SECURITY INVOKER
->   BEGIN
->     DECLARE sofm DATE;
->     SET sofm = SUBDATE (
->         curdate,
->         INTERVAL SECOND(curdate) SECOND
->       );
->   RETURN sofm;
->   END //
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use near ');
RETURN sofm;
END' at line 11

更令人沮丧的是,以下工作:(

mysql> CREATE FUNCTION start_of_week(
->     curdate DATE,
-> first_day_of_week INTEGER)
->    RETURNS DATE
->    DETERMINISTIC
->    SQL SECURITY INVOKER
->    BEGIN
->       DECLARE sow DATE;
->       SET sow = SUBDATE(
->         curdate,
->         INTERVAL (WEEKDAY(curdate)+(7-first_day_of_week)%7) DAY
->         );
->       RETURN sow;
->    END //
Query OK, 0 rows affected (0.00 sec)

注意可能还有其他方式来截断日期在各个级别 - 我当然有兴趣听到它们,我真的想知道我的语法有什么问题 - 不是计算周期开始的不同方法)。

是的, second() 是一个有效的函数,以及第二是一个有效的区间

TIA

(see my answer below for solution - thanks for the feedback)

It's probably something really obvious but I can't see what's wrong with my sql:

mysql> CREATE FUNCTION start_of_minute(
->     curdate DATE)
->   RETURNS DATE
->   DETERMINISTIC
->   SQL SECURITY INVOKER
->   BEGIN
->     DECLARE sofm DATE;
->     SET sofm = SUBDATE (
->         curdate,
->         INTERVAL SECOND(curdate) SECOND
->       );
->   RETURN sofm;
->   END //
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use near ');
RETURN sofm;
END' at line 11

All the more frustrating as the following works:

mysql> CREATE FUNCTION start_of_week(
->     curdate DATE,
-> first_day_of_week INTEGER)
->    RETURNS DATE
->    DETERMINISTIC
->    SQL SECURITY INVOKER
->    BEGIN
->       DECLARE sow DATE;
->       SET sow = SUBDATE(
->         curdate,
->         INTERVAL (WEEKDAY(curdate)+(7-first_day_of_week)%7) DAY
->         );
->       RETURN sow;
->    END //
Query OK, 0 rows affected (0.00 sec)

(NB there may be other ways of truncating the date at various levels - and I'd certainly be interested in hearing about them, I really want to know what's wrong with my syntax - not a different method for calculating the start of a period).

Yes, second() is a valid function, and SECOND is a valid interval.

TIA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

虚拟世界 2024-10-19 13:26:14

我已经考虑到它对类型感到不安 - 但是用“DATETIME”类型替换所有“日期”类型并没有解决问题 - 结果问题是“SUBDATE”和“(”之间的空格 - 我从来没有知道 MySQL 对这些事情很挑剔

(我会将此标记为答案,但所以希望我先等几天)

I had already considered that it was getting upset about the types - however replacing all the 'date' types with 'DATETIME' types did not resolve the problem - turns out the problem was a space between 'SUBDATE' and '(' - I never knew MySQL was fussy about such things!

(I'd flag this as an asnwer but SO wants me to wait a couple of days first)

长发绾君心 2024-10-19 13:26:14

curdate 是保留字:

drop function if exists start_of_minute;

delimiter #

create function start_of_minute
(
p_curdate datetime
)
returns datetime
begin
declare sofm datetime;
  set sofm = subdate(p_curdate, interval second(p_curdate) second);
  return sofm;
end#

delimiter;
select now(), start_of_minute(now());

编辑

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2011-01-17 |
+------------+
1 row in set (0.00 sec)

curdate is a reserved word:

drop function if exists start_of_minute;

delimiter #

create function start_of_minute
(
p_curdate datetime
)
returns datetime
begin
declare sofm datetime;
  set sofm = subdate(p_curdate, interval second(p_curdate) second);
  return sofm;
end#

delimiter;
select now(), start_of_minute(now());

EDIT

mysql> select curdate();
+------------+
| curdate()  |
+------------+
| 2011-01-17 |
+------------+
1 row in set (0.00 sec)
天邊彩虹 2024-10-19 13:26:14

问题是因为函数 second() 接受的是 time 而不是 date

可能像这样

second( cast(curdate as time) )

BUT

是没有意义的将给定日期的日期细分为秒,并以日期格式返回(仍然是日期)

The problem is because the function second() take in time instead of date

maybe like this

second( cast(curdate as time) )

BUT

is meaningless due to sub-date for a given date to seconds, and return in date format (which still a date)

时常饿 2024-10-19 13:26:14

您不应该使用 DATETIME 数据类型而不是 DATE 吗?

Should you not use a DATETIME data type rather than DATE?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文