努力查看我的方法的错误(mysql函数)
(请参阅下面我的答案以获取解决方案 - 感谢您的反馈)
这可能是非常明显的事情,但我看不出我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已经考虑到它对类型感到不安 - 但是用“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)
curdate 是保留字:
编辑
curdate is a reserved word:
EDIT
问题是因为函数
second()
接受的是time
而不是date
可能像这样
BUT
是没有意义的将
给定日期的日期细分为秒
,并以日期格式返回(仍然是日期)The problem is because the function
second()
take intime
instead ofdate
maybe like this
BUT
is meaningless due to sub-date for
a given date to seconds
, and return in date format (which still a date)您不应该使用 DATETIME 数据类型而不是 DATE 吗?
Should you not use a DATETIME data type rather than DATE?