来自MySQL的结果集不一致

时间:2012-08-31 17:38:55

标签: mysql

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

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

我正在编写这个存储过程中来自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_task时,我得到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)

AS REQUESTED:完整存储的PROC代码

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 ;

1 个答案:

答案 0 :(得分:1)

发表评论作为回答:

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