douboshan1466 2013-05-23 11:18
浏览 44
已采纳

我如何计算平均评级并在我的MySQL表中添加一个新列来记录这一点

I have a MySQL table of product ID's which has columns which show how many times each of these products has recieved a 1, 2, 3, 4 or 5 star rating from my customers. I would like to insert into the average rating column, which is currently blank, a figure to indicate what the average rating for each product is.

How can I calculate what the average rating for each product ID is and then modify the table to insert this average rating into the average rating column? I would then ideally like to run this SQL statement daily using a php script and a cron job to recalculate the average rating and update this table as more ratings are recorded for each product.

An example of the layout of the table is below:

Product ID | 1 | 2 | 3 | 4 | 5 | Average Rating

1294518    | 0 | 3 | 3 | 2 | 4 | ?

9226582    | 0 | 0 | 1 | 0 | 0 | ?

3946583    | 0 | 3 | 6 | 1 | 0 | ?

7392588    | 1 | 0 | 0 | 0 | 0 | ?

1196585    | 0 | 1 | 0 | 5 | 6 | ?

4285385    | 0 | 2 | 3 | 3 | 1 | ?
  • 写回答

1条回答 默认 最新

  • doujuan2688 2013-05-23 11:21
    关注

    Have you tried:

    UPDATE table
    SET aveRate = (rate1 + rate2*2  + rate3*3  + rate4*4 + rate5*5) /
                               (rate1 + rate2 + rate3 + rate4 + rate5)
    

    Am I missing something?

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

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP