1

Not sure how to remove this, but this has been solved. Thanks!

EDIT: as requested, full stored proc code is at the bottom

I'm getting a very odd issue with inconsistent result sets from mysql in this stored procedure that I'm writing. I've included the debug statements I'm using and their outputs:

Statements (these are all on consecutive lines / appear in code exactly as written):

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';

The result I get is:

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)

Basically, even though I can see that the event_ids are 1 when I do select * from event_task, when I re-query event_task for event_id as a specific column I get NULL values.

edit: just selected out the user ID with the following:

SELECT 'SELECT user_id';
SELECT user_id;

The result is:

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: FULL STORED PROC CODE

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 に答える