MySQL 5.6.21存储过程SQL错误1064

标签 mysql sql database stored-procedures

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

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 

enter image description here

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

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 Error (1064): 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 'END' at line 41

我已阅读相关问题(包括以下内容),但对解决问题没有帮助。

Mysql stored procedure error

Mysql stored procedure error 1064

附注选择版本();返回5.6.21

最佳答案

尝试:

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 ;

关于MySQL 5.6.21存储过程SQL错误1064,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45115426/

相关文章:

mysql - 使用 Selenium 和 Testng 通过 Eclipse 从 Excel 将数据插入 MySQL

mysql - 如何将一张表与另一张表多次连接?

sql - PostgreSQL:对预矢量化数据库执行余弦相似性搜索

mysql在where条件下按列分组

mysql - 如何在 MySQL 中进行相交查询?

java - Spring JDBCTemplate ResultSetExtractor 和 Spring Batch ItemReader 之间的区别

sql - 将数据库从服务器复制到本地数据库(我只需要复制结构,没有数据)

python - 使用 Flask SQLAlchemy 从 SQLite 切换到 MySQL

java - 如何防止 JPA 实体出现 ClassCastException?

php - 在现有网站上使用 Laravel