0

我有一个单独工作的 SELECT 语句,并返回约 8000 行。我正在尝试将它作为游标添加到存储过程中,但它没有返回任何内容。

你能发现我错过了什么吗?

作品

SELECT a.customer, a.order_id
FROM temp_orders a
INNER JOIN (
    SELECT customer, MAX(order_id) AS last_order
    FROM temp_orders
    WHERE pay_status = 3
       OR pay_status = 4
    GROUP BY customer
) AS b ON  a.customer = b.customer
       AND a.order_id = b.last_order
WHERE pay_status = 3
   OR pay_status = 4
ORDER BY a.customer;

不再工作

DELIMITER //

DROP PROCEDURE IF EXISTS get_last_orders //
CREATE PROCEDURE get_last_orders()

    BEGIN

        DECLARE order_id VARCHAR(15);
        DECLARE customer_id INT;
        DECLARE done INT DEFAULT 0;

        DECLARE cur1 CURSOR FOR SELECT a.customer, a.order_id
                                FROM temp_orders a
                                INNER JOIN (
                                    SELECT customer, MAX(order_id) AS last_order
                                    FROM temp_orders
                                    WHERE pay_status = 3
                                       OR pay_status = 4
                                    GROUP BY customer
                                ) AS b ON  a.customer = b.customer
                                       AND a.order_id = b.last_order
                                WHERE pay_status = 3
                                   OR pay_status = 4
                                ORDER BY a.customer;

        DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

        DROP TABLE IF EXISTS temp_last_orders;

        CREATE TABLE temp_last_orders (
            id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
            order_id VARCHAR(15) NOT NULL,
            customer_id INT NOT NULL
        ) ENGINE = MYISAM
        COMMENT = 'Last paid order from each customer';

        OPEN cur1;
            read_loop: LOOP
                FETCH cur1 INTO order_id, customer_id;
                IF done THEN
                    LEAVE read_loop;
                END IF;

                INSERT INTO temp_last_orders (order_id, customer_id) VALUES (order_id, customer_id);

            END LOOP;
        CLOSE cur1;

    END; //

CALL get_last_orders();
4

2 回答 2

1

具有相同名称的变量和列名存在问题。尝试将变量 order_id 和 customer_id 重命名为例如 v_order_id 和 v_customer_id 并试一试。

于 2013-10-18T11:37:22.860 回答
0

解决问题的另一种方法:

DELIMITER //

DROP PROCEDURE IF EXISTS get_last_orders //
CREATE PROCEDURE get_last_orders()

BEGIN

    DROP TABLE IF EXISTS temp_last_orders;

    CREATE TABLE temp_last_orders (
        id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        order_id VARCHAR(15) NOT NULL,
        customer_id INT NOT NULL
    ) ENGINE = MYISAM
    COMMENT = 'Last paid order from each customer';

    INSERT INTO temp_last_orders (order_id, 
                                  customer_id)
    SELECT a.customer, 
           a.order_id
    FROM temp_orders a
    INNER JOIN (
                 SELECT customer, 
                        MAX(order_id) AS last_order
                 FROM temp_orders
                 WHERE pay_status = 3
                    OR pay_status = 4
                 GROUP BY customer
               ) AS b 
           ON  a.customer = b.customer
          AND a.order_id = b.last_order
    WHERE pay_status = 3
       OR pay_status = 4
    ORDER BY a.customer;

END; //

CALL get_last_orders();
于 2013-10-18T11:52:09.677 回答