download201401 2011-06-19 19: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-19 21: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!

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部