MySQL 游标未在过程内获取

发布于 2024-12-03 19:42:53 字数 2451 浏览 1 评论 0原文

我有以下表

albums:

+----------------+-------------+------+-----+---------+----------------+
| 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,它会显示为 null。

I have the following tables

albums:

+----------------+-------------+------+-----+---------+----------------+
| 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    |                |
+----------------+-------------+------+-----+---------+----------------+

and 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    |                |
+-----------------+---------+------+-----+---------+----------------+

After every insert into the *music_rating* I want to update the average rating in the albums table. I have a trigger for this, which calls a procedure. The thing is, the procedure does not work, for some reason the cursor is not fetching data from the table. (I called the procedure separately to make sure it isn't the trigger acting up. The tables have a couple of rows already, so it's not that.)

My procedure is pretty straight forward and looks like this

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 ;

I echoed the result of the cursor with a SELECT rating after the FETCH cur INTO rating; command, and it shows up as null.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫思念不要吵 2024-12-10 19:42:53

您无需计算 avg_ rating 并将其存储在 albums 表中。您可以即时计算 -

SELECT a.album_id, a.name, AVG(mr.rating) FROM albums a
  LEFT JOIN music_ratings mr
    ON a.album_id = mr.album_id
GROUP BY a.album_id

You do not need to calculate and store avg_rating in the albums table. You can calculate in on the fly -

SELECT a.album_id, a.name, AVG(mr.rating) FROM albums a
  LEFT JOIN music_ratings mr
    ON a.album_id = mr.album_id
GROUP BY a.album_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文