1

不知道如何删除它,但这已经解决了。谢谢!

编辑:根据要求,完整存储的过程代码位于底部

在我正在编写的这个存储过程中,我遇到了一个非常奇怪的问题,即来自 mysql 的结果集不一致。我已经包含了我正在使用的调试语句及其输出:

语句(这些都在连续的行中/完全按照书面形式出现在代码中):

SELECT 'SELECT * FROM event_task';
SELECT * from event_task;
SELECT 'SELECT event_id FROM event_task';
SELECT event_id FROM event_task;
SELECT 'SELECT * FROM event_task WHERE event_id = 1';
SELECT * FROM event_task WHERE event_id = 1;
SELECT 'end select';

我得到的结果是:

mysql> call get_user_events(1);
+--------------------------+
| SELECT * FROM event_task |
+--------------------------+
| SELECT * FROM event_task |
+--------------------------+
1 row in set (0.01 sec)

+----+----------+---------+--------+----------+---------------------+---------------------+
| id | event_id | user_id | task   | complete | created_at          | updated_at          |
+----+----------+---------+--------+----------+---------------------+---------------------+
|  1 |        1 |       1 | stuff  |        0 | 2012-08-30 00:00:00 | 2012-08-30 00:00:00 |
|  2 |        1 |       2 | stuff2 |        1 | 2012-08-30 00:00:00 | 2012-08-30 00:00:00 |
+----+----------+---------+--------+----------+---------------------+---------------------+
2 rows in set (0.01 sec)

+---------------------------------+
| SELECT event_id FROM event_task |
+---------------------------------+
| SELECT event_id FROM event_task |
+---------------------------------+
1 row in set (0.01 sec)

+----------+
| event_id |
+----------+
|     NULL |
|     NULL |
+----------+
2 rows in set (0.01 sec)

+---------------------------------------------+
| SELECT * FROM event_task WHERE event_id = 1 |
+---------------------------------------------+
| SELECT * FROM event_task WHERE event_id = 1 |
+---------------------------------------------+
1 row in set (0.01 sec)

Empty set (0.01 sec)

+------------+
| end select |
+------------+
| end select |
+------------+
1 row in set (0.01 sec)

基本上,即使当我从 event_task 中选择 * 时我可以看到 event_ids 为 1,但当我重新查询 event_task 的 event_id 作为特定列时,我得到 NULL 值。

编辑:刚刚选择了以下用户ID:

SELECT 'SELECT user_id';
SELECT user_id;

结果是:

mysql> call get_user_events(1);
+----------------+
| SELECT user_id |
+----------------+
| SELECT user_id |
+----------------+
1 row in set (0.00 sec)

+---------+
| user_id |
+---------+
|       1 |
+---------+
1 row in set (0.00 sec)

根据要求:完整的存储过程代码

DELIMITER $$
DROP PROCEDURE IF EXISTS get_user_events$$
CREATE PROCEDURE get_user_events (IN user_id int) 
/*
gets the user's events for the default page
grabs: image location, event title, date, location, number of tasks completed / total, and updates
*/
BEGIN
    DECLARE done TINYINT(1) DEFAULT FALSE;
    DECLARE loop_cnt INT DEFAULT 0;
    DECLARE num_rows INT DEFAULT 0;
    DECLARE event_id INT;

    DECLARE update_cur CURSOR FOR
        SELECT DISTINCT id FROM aggregate;

    DECLARE CONTINUE HANDLER FOR NOT FOUND
        SET done = TRUE;

    DROP TABLE IF EXISTS tasks;
    DROP TABLE IF EXISTS aggregate;
    DROP TABLE IF EXISTS updates;
    CREATE TEMPORARY TABLE tasks (id int, event_id int, complete tinyint(1));
    CREATE TEMPORARY TABLE aggregate (id int, picture_location varchar(255), name varchar(255), date timestamp, 
        place varchar(255), num_tasks int, tasks_complete int, status_update varchar(5000));
    CREATE TEMPORARY TABLE updates (event_id int, status_update varchar(255));

    /*get basic event information*/
    INSERT INTO aggregate (id, picture_location, name, date, place)
    SELECT e.id,
           e.picture_location, 
           e.name, 
           ed.date,
           ep.place
    FROM event e INNER JOIN event_user_map eum ON e.id = eum.event_id
                 LEFT JOIN event_place ep ON e.id = ep.event_id AND ep.vote_final = 1
                 LEFT JOIN event_date ed ON e.id = ed.event_id AND ed.vote_final = 1
    WHERE eum.user_id = user_id;

    /*grab the list of tasks so we can work with a small subset*/
    INSERT INTO tasks 
        SELECT id, event_id, complete 
        FROM event_task 
        WHERE event_id IN (SELECT DISTINCT id FROM aggregate);

    SELECT 'SELECT * FROM event_task';
    SELECT * from event_task;
    SELECT 'SELECT event_id FROM event_task';
    SELECT event_id FROM event_task;
    SELECT 'SELECT * FROM event_task WHERE event_id = 1';
    SELECT * FROM event_task WHERE event_id = 1;
    SELECT 'SELECT event_id FROM (SELECT * FROM event_task) as A';
    SELECT event_id FROM (SELECT * FROM event_task) as A;
    SELECT 'end select';

    /*gets tasks*/
    UPDATE aggregate SET num_tasks = (SELECT COUNT(*) FROM tasks WHERE event_id = id);
    UPDATE aggregate SET tasks_complete = (SELECT COUNT(*) FROM tasks WHERE event_id = id AND complete = 1);

    /*get updates*/
    OPEN update_cur;

    read_loop: LOOP
        FETCH update_cur INTO event_id;
        IF done THEN
            LEAVE read_loop;
        END IF;

        INSERT INTO updates
            SELECT event_id, status_update FROM event_update ORDER BY updated_at limit 3;
    END LOOP;

    UPDATE aggregate SET status_update = (SELECT group_concat(status_update) FROM updates u WHERE u.event_id = id);

    SELECT * FROM aggregate;

END$$
DELIMITER ;
4

1 回答 1

1

发表我的评论作为答案:

更改DECLARE event_id INT;DECLARE event_id_var INT;并运行您的存储过程

于 2012-08-31T18:57:34.303 回答