<分区>
我有 2 个表,Ratings
和 Recipes
。
在 Ratings
上插入后,我需要找到评分食谱的所有评分的平均值,并更新 Recipes
中的 Rating_Avg
列表。
这行得通,但我相信它正在更新 Recipes.Rating_Avg
中的所有行,而我只需要更新其中 Recipe_No = 最近评级的 Recipe_No 的行。
CREATE TRIGGER `update_avg` AFTER INSERT ON `Ratings`
FOR EACH ROW UPDATE Recipes
SET Rating_Avg = (SELECT AVG(Rating) from Ratings where Ratings.Recipe_No=Recipes.Recipe_No)
我觉得我需要添加一个 WHERE Recipe_No = NEW.Recipe_No
但我不确定在哪里添加它。