download201401 2011-06-20 03:41 采纳率: 0%
浏览 44
已采纳

如何创建一个mysql_query来拉动使用贝叶斯平均值排名的项目?

Given a database table with the following fields:

  • total_votes: The total number of votes cast on this item/row
  • average_value: The average voting value (i.e. 3.6 with 5 being the max)
  • total_value: The sum of all the vote values (i.e. 3 votes of 4 will result in 12)

I was simply using a SELECT to grab the rows and the ORDER BY to sort them by their average value but then I ran into the issue of a good row with 10 votes averaging 4.9 getting outranked by a brand new row with only 1 vote of 5 outranking it.

After some research, I discovered that the most popular method of sorting rated items is to use a bayesian average.

br = ( (avg_num_votes * avg_rating) + (this_num_votes * this_rating) ) / (avg_num_votes + this_num_votes)

However, I'm having difficulty creating a query that pulls these rows and sorts them using this average as I'm a novice at creating anything advanced in terms of mySQL queries.

What I'm looking for is:

$results = mysql_query("INSERT ANSWER HERE");

Where INSERT ANSWER HERE is the answer you provide me with and $results provides these items sorted in by their Bayesian Average.

For more information about the Bayesian Average, I checked out: http://www.thebroth.com/blog/118/bayesian-rating

  • 写回答

1条回答 默认 最新

  • dongxianghui3709 2011-06-20 05:49
    关注

    A simple way would be to use two queries. Perhaps this will do the trick

    Query #1

    SELECT
        AVG(total_votes) AS avg_num_votes,
        AVG(average_value) AS avg_rating
    FROM
        MyThings
    WHERE
        total_votes > 0
    

    This should give you the avg_num_votes for all items (that have at least one vote), and avg_rating across all items (again for those with at least one vote).

    With these values in hand, construct query #2 and use ORDER BY:

    Query #2

    SELECT
        thing_id
        thing_name
    FROM
        MyThings
    ORDER BY
        (($avg_num_votes * $avg_rating) + (total_votes * average_value)) / ($avg_num_votes + total_votes)
    

    Before submitting this query, replace $avg_num_votes and $avg_rating with the results from query #1.

    Warning: Haven't tested this, and I'm not familiar with Bayesian ranking.

    Hope that helps!

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。