douwo8140 2015-07-28 19:47
浏览 37
已采纳

如何将recipe_id不同的所有评级列值相加

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).

Expected result is the recipe_id, where all ratings associated with that recipe_id are the highest.

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.

  • 写回答

1条回答 默认 最新

  • duanhe4267 2015-07-28 19:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料