【问题标题】:MySQL 5.6.21 Stored procedure SQL error 1064MySQL 5.6.21 存储过程 SQL 错误 1064
【发布时间】:2017-07-15 06:58:49
【问题描述】:

我有这个查询,它运行良好并给出了预期的结果。

SELECT count(*) AS total_unsolved,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) = 0 and response_time is null) as today_to_solve,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 0 and datediff(deadline, CURTIME()) < 4 and response_time is null) as days_left_1_3,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 3 and datediff(deadline, CURTIME()) < 8 and response_time is null) as days_left_4_7,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 7 and response_time is null) as mt_week,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) < 0 and datediff(deadline, CURTIME()) > -4 and response_time is null) as overdue_1_3_days,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -3 and datediff(deadline, CURTIME()) > -11 and response_time is null) as overdue_4_10_days,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -10 and datediff(deadline, CURTIME()) > -30 and response_time is null) as overdue_11_30_days,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -30 and response_time is null) as overdue_mt_month,
    (select count(deadline) from my_table where datediff(deadline, response_time) < 0 and response_time is not null) as solved_with_delay  
 from my_table where response_time is null 

我想创建一个在给定时间范围内显示结果的过程。 所以我输入了以下内容:

CREATE PROCEDURE status_in_timerange
     (
        IN   start_date                     TiMESTAMP, 
        IN   close_date                     TiMESTAMP,
        OUT  total_unsolved                 INT,  
        OUT  today_to_solve                 INT, 
        OUT  days_left_1_3                  INT, 
        OUT  days_left_4_7                  INT, 
        OUT  mt_week                        INT, 
        OUT  overdue_1_3_days               INT, 
        OUT  overdue_4_10_days              INT,      
        OUT  overdue_11_30_days             INT,    
        OUT  overdue_mt_month               INT,    
        OUT  solved_with_delay              INT    
     )
BEGIN 

    SELECT count(DATEDIFF(deadline, NOW())) AS total_unsolved,
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) = 0 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 0 and datediff(deadline, CURTIME()) < 4 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 3 and datediff(deadline, CURTIME()) < 8 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 7 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) < 0 and datediff(deadline, CURTIME()) > -4 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -3 and datediff(deadline, CURTIME()) > -11 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -10 and datediff(deadline, CURTIME()) > -30 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -30 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, response_time) < 0 and response_time is not null and ktimestamp BETWEEN start_date and close_date) 

    INTO   total_unsolved , 
           today_to_solve, 
           days_left_1_3, 
           days_left_4_7, 
           mt_week, 
           overdue_1_3_days ,
           overdue_4_10_days,
           overdue_11_30_days,
           overdue_mt_month,
           solved_with_delay

  FROM my_table WHERE response_time is null and ktimestamp BETWEEN start_date and close_date
END ;

很遗憾,此查询显示如下错误:

SQL 错误 (1064):您的 SQL 语法有错误;检查 与您的 MySQL 服务器版本相对应的手册 在第 41 行的“END”附近使用的语法

我已阅读无助于解决问题的相关问题(包括以下内容)。

Mysql stored procedure error

Mysql stored procedure error 1064

附: select version(); 返回 5.6.21

【问题讨论】:

  • 请帮我从存储过程中找出错误
  • 在语句末尾添加分号 (;):... WHERE response_time is null and ktimestamp BETWEEN start_date and close_date;
  • @sForSujit 我浏览了解释错误 1064 的常见情况的文章和视频。但我在存储过程中找不到任何这些错误。如果你能看到,请给我提示
  • @wchiquito 在此时使用分隔符导致该行出现新错误: SQL 错误(1064):您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以了解在第 40 行的“附近使用正确的语法

标签: mysql sql database stored-procedures


【解决方案1】:

试试:

mysql> DELIMITER //

mysql> CREATE PROCEDURE status_in_timerange
    ->      (
    ->         IN   start_date                     TiMESTAMP, 
    ->         IN   close_date                     TiMESTAMP,
    ->         OUT  total_unsolved                 INT,  
    ->         OUT  today_to_solve                 INT, 
    ->         OUT  days_left_1_3                  INT, 
    ->         OUT  days_left_4_7                  INT, 
    ->         OUT  mt_week                        INT, 
    ->         OUT  overdue_1_3_days               INT, 
    ->         OUT  overdue_4_10_days              INT,      
    ->         OUT  overdue_11_30_days             INT,    
    ->         OUT  overdue_mt_month               INT,    
    ->         OUT  solved_with_delay              INT    
    ->      )
    -> BEGIN 
    ->     SELECT count(DATEDIFF(deadline, NOW())) AS total_unsolved,
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) = 0 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 0 and datediff(deadline, CURTIME()) < 4 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 3 and datediff(deadline, CURTIME()) < 8 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 7 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) < 0 and datediff(deadline, CURTIME()) > -4 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -3 and datediff(deadline, CURTIME()) > -11 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -10 and datediff(deadline, CURTIME()) > -30 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -30 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, response_time) < 0 and response_time is not null and ktimestamp BETWEEN start_date and close_date) 
    ->     INTO   total_unsolved , 
    ->            today_to_solve, 
    ->            days_left_1_3, 
    ->            days_left_4_7, 
    ->            mt_week, 
    ->            overdue_1_3_days ,
    ->            overdue_4_10_days,
    ->            overdue_11_30_days,
    ->            overdue_mt_month,
    ->            solved_with_delay
    ->   FROM my_table
    ->   WHERE response_time is null and
    ->         ktimestamp BETWEEN start_date and close_date;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 2018-06-21
    • 2012-08-02
    • 2014-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多