0
我有以下表MySQL的光標不取裏面的程序
專輯:
+----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+----------------+
| album_id | int(11) | NO | PRI | NULL | auto_increment |
| band_id | int(11) | YES | MUL | NULL | |
| release_date | varchar(45) | YES | | NULL | |
| name | varchar(45) | YES | | NULL | |
| format | varchar(45) | YES | | NULL | |
| music_genre_id | int(11) | YES | MUL | NULL | |
| label_id | int(11) | YES | MUL | NULL | |
| avg_rating | float | YES | | NULL | |
+----------------+-------------+------+-----+---------+----------------+
和music_ratings
+-----------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------+------+-----+---------+----------------+
| music_rating_id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | YES | MUL | NULL | |
| album_id | int(11) | YES | MUL | NULL | |
| rating | int(11) | YES | | NULL | |
+-----------------+---------+------+-----+---------+----------------+
每一個後插入* music_rating *我想更新平均評分在相冊列表中。我有一個觸發器,它調用一個過程。事情是,該過程不起作用,出於某種原因,遊標不從表中獲取數據。 (我單獨調用的程序,以確保它不是觸發演戲了。該表有一對夫婦行了,所以它不是。)
我的程序是非常簡單的,看起來像這樣
DELIMITER $$
CREATE PROCEDURE avg_album_calc(IN id_album INT)
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE rating INT;
DECLARE cur CURSOR FOR SELECT `rating` FROM `music_ratings` WHERE `album_id`=id_album;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur;
SET @ct=0;
SET @sm=0;
REPEAT
FETCH cur INTO rating;
IF NOT done
THEN
SET @ct = @ct +1;
SET @sm = @sm + rating;
END IF;
UNTIL done END REPEAT;
UPDATE albums SET avg_rating = @sm/@ct WHERE album_id = id_album;
CLOSE cur;
END$$
DELIMITER ;
我在FETCH cur INTO rating;
命令後附加了SELECT rating
的遊標結果,它顯示爲空。
你是對的,我必須使用一個程序,雖然:) – adamors