简体   繁体   English

如何对配方_id不同的所有评级列值求和

[英]How to sum all rating column values where recipe_id is distinct

To be more specific I have a table with recipes. 更具体地说,我有一张包含食谱的表格。 In another table I keep ratings associated with these recipes. 在另一个表格中,我保留与这些食谱相关的评分。 Each rating has an unique id, the second column is the recipe's id, the third column is the rating (from 1-5). 每个等级都有一个唯一的ID,第二列是配方的ID,第三列是等级(从1-5开始)。

Expected result is the recipe_id, where all ratings associated with that recipe_id are the highest. 预期的结果是“ recipe_id”,其中与该“ recipe_id”关联的所有评分均为最高。

I hope you understand, I have no idea how to approach such query properly. 希望您理解,我不知道如何正确处理此类查询。

Hopefully I've posted my question properly, it's my first one. 希望我已经正确地发布了我的问题,这是我的第一个问题。

You could use a self-join: 您可以使用自联接:

SELECT
  r.id,
  r.recipe_id,
  r.rating
FROM
  recipes_ratings r LEFT JOIN recipes_ratings r2
  ON r.recipe_id = r2.recipe_id
     AND r2.rating>r.rating
WHERE
  r2.rating IS NULL

Please see a fiddle here . 请看这里的小提琴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM