在mysql中创建错误的程序

时间:2014-08-13 14:44:45

标签: php mysql procedure

我正在尝试在mysql中执行查询。所以,当我跑步时,我会收到:

Error Code: 1304. PROCEDURE ander already exists

我尝试用@定义变量“a”,但结果相同 - 错误代码:1304。 实际上当我执行SELECT语句是正确的。那么,哪里可能是我的错?

DELIMITER //
CREATE PROCEDURE ander() 

BEGIN


DECLARE a int ;
SET a=1;


  WHILE a < 20
    DO

    SELECT 
    (SUM(price) + SUM(delivery_price)) as total_sum,
    COUNT(`id`) AS total_cnt,
    market_id,
    `date`,
    COUNT(`user_id`) AS total_users
    FROM orders 
    WHERE 
    (`date`>='2014-08-01 00:00:00' - INTERVAL a DAY) AND ( `date`<='2014-08-01   23:59:59' - INTERVAL a DAY)

    GROUP BY market_id;


    SET a=a+1;


    END WHILE;

END//
DELIMITER ; 

谢谢!

2 个答案:

答案 0 :(得分:1)

在开头添加:

DROP PROCEDURE ander;

所以你应该:

DROP PROCEDURE ander;

CREATE PROCEDURE ander() 

BEGIN


DECLARE a int ;
SET a=1;


  WHILE a < 20
    DO

    SELECT 
    (SUM(price) + SUM(delivery_price)) as total_sum,
    COUNT(`id`) AS total_cnt,
    market_id,
    `date`,
    COUNT(`user_id`) AS total_users
    FROM orders 
    WHERE 
    (`date`>='2014-08-01 00:00:00' - INTERVAL a DAY) AND ( `date`<='2014-08-01   23:59:59' - INTERVAL a DAY)

    GROUP BY market_id;


    SET a=a+1;


    END WHILE;

END//
DELIMITER ; 

答案 1 :(得分:1)

每个mysql PROCEDURE都存储在数据库中(基本上它是一个数据库对象)

您需要先使用语法

删除
DROP PROCEDURE ander

文档: