Trying to use DATEADD and getting function does not exist error
我有表
我想用这个表做的是替换
我应该能够指定
这是我目前使用的查询...
1 2 3 4 |
但是它产生了这个错误信息......
1 | #1305 - FUNCTION DatabaseName.DATEADD does not exist |
此查询可用于创建我拥有的表...
1 2 3 4 5 |
我使用以下示例数据填充上表...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | INSERT INTO TableName ( product_id, ScheduleTime ) VALUES ( '01', '2017-05-07 22:00:59' ), ( '02', '2017-05-07 09:09:59' ), ( '03', '2017-05-07 09:59:59' ), ( '04', '2017-05-07 09:49:59' ), ( '05', '2017-05-07 23:09:59' ), ( '06', '2017-05-07 23:09:59' ), ( '07', '2017-05-07 23:09:59' ), ( '08', '2017-05-07 23:09:59' ), ( '09', '2017-05-07 23:09:59' ), ( '10', '2017-05-07 23:09:59' ), ( '11', '2017-05-07 23:09:59' ), ( '12', '2017-05-07 23:09:59' ), ( '13', '2017-05-07 23:09:59' ), ( '14', '2017-05-07 23:09:59' ), ( '15', '2017-05-07 23:09:59' ), ( '16', '2017-05-07 23:09:59' ), ( '17', '2017-05-07 23:09:59' ), ( '18', '2017-05-07 23:09:59' ), ( '19', '2017-05-07 23:09:59' ), ( '20', '2017-05-07 23:09:59' ); |
请尝试以下......
1 2 3 4 5 6 7 8 9 10 11 12 | SET @workingScheduleTime := ( SELECT DATE_ADD( '2017-05-06 22:00:59', INTERVAL -20 MINUTE ) ); UPDATE TableName JOIN ( SELECT product_id, CASE WHEN TIME( DATE_ADD( @workingScheduleTime, INTERVAL 20 MINUTE ) ) NOT BETWEEN '05:00:01' and '22:29:59' THEN @workingScheduleTime := DATE_ADD( TIMESTAMP( DATE( @workingScheduleTime ), '05:00:01' ), INTERVAL 1 DAY ) ELSE @workingScheduleTime := DATE_ADD( @workingScheduleTime, INTERVAL 20 MINUTE ) END AS ScheduleTime FROM TableName ) AS redatedList ON TableName.product_id = redatedList.product_id SET TableName.ScheduleTime = redatedList.ScheduleTime; |
此解决方案通过循环遍历每个记录并将
在我们的工作值初始化的情况下,我使用基于
1 2 3 4 5 6 7 8 9 10 11 | UPDATE TableName JOIN ( SELECT product_id, CASE WHEN TIME( DATE_ADD( @workingScheduleTime, INTERVAL 20 MINUTE ) ) NOT BETWEEN '05:00:01' and '22:29:59' THEN @workingScheduleTime := DATE_ADD( TIMESTAMP( DATE( @workingScheduleTime ), '05:00:01' ), INTERVAL 1 DAY ) ELSE @workingScheduleTime := DATE_ADD( @workingScheduleTime, INTERVAL 20 MINUTE ) END AS ScheduleTime FROM TableName ) AS redatedList ON TableName.product_id = redatedList.product_id SET TableName.ScheduleTime = redatedList.ScheduleTime; |
此语句首先在
1 2 3 4 5 6 7 8 | SELECT product_id, CASE WHEN TIME( DATE_ADD( @workingScheduleTime, INTERVAL 20 MINUTE ) ) NOT BETWEEN '05:00:01' and '22:29:59' THEN @workingScheduleTime := DATE_ADD( TIMESTAMP( DATE( @workingScheduleTime ), '05:00:01' ), INTERVAL 1 DAY ) ELSE @workingScheduleTime := DATE_ADD( @workingScheduleTime, INTERVAL 20 MINUTE ) END AS ScheduleTime FROM TableName |
此子查询为
然后选择
如上所述,子查询的结果然后以这样的方式连接到
此答案针对使用问题中的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | INSERT INTO TableName ( product_id, ScheduleTime ) VALUES ( '01', '2017-05-07 22:00:59' ), ( '02', '2017-05-07 09:09:59' ), ( '03', '2017-05-07 09:59:59' ), ( '04', '2017-05-07 09:49:59' ), ( '05', '2017-05-07 23:09:59' ), ( '06', '2017-05-07 23:09:59' ), ( '07', '2017-05-07 23:09:59' ), ( '08', '2017-05-07 23:09:59' ), ( '09', '2017-05-07 23:09:59' ), ( '10', '2017-05-07 23:09:59' ), ( '11', '2017-05-07 23:09:59' ), ( '12', '2017-05-07 23:09:59' ), ( '13', '2017-05-07 23:09:59' ), ( '14', '2017-05-07 23:09:59' ), ( '15', '2017-05-07 23:09:59' ), ( '16', '2017-05-07 23:09:59' ), ( '17', '2017-05-07 23:09:59' ), ( '18', '2017-05-07 23:09:59' ), ( '19', '2017-05-07 23:09:59' ), ( '20', '2017-05-07 23:09:59' ); |
我的代码的结果已被提问者确认为合适。
进一步阅读
https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between(在MySQL的
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date(在MySQL的
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add(在MySQL的
https://dev.mysql.com/doc/refman/5.7/en/set-statement.html(在MySQL的
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_time(在MySQL的
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_timestamp(在MySQL的
https://dev.mysql.com/doc/refman/5.7/en/update.html(在MySQL的