2

我想根据票数创建一个权重的评级。所以,1 票 5 票不会好于 4 票票 4 票。

我找到了这个数学表格:

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

如何进行 MySQL SELECT 以获得最佳评级图像的 ID。

我有一张 IMAGE 表和一张 VOTING 表

表决:

id
imageID
totalVotes
avgVote

我想我必须在 SELECT 中使用 SELECT 来执行此操作,但是如何操作?

4

1 回答 1

1

第一步是计算avg_num_votesavg_rating

SELECT
  SUM(totalVotes)/COUNT(*) AS avg_num_votes,
  SUM(avgVote)/COUNT(*) AS avg_rating
FROM voting;

如果您可以忍受一个小错误,那么偶尔计算一次可能就足够了。

现在使用您的公式和上面的值,您可以运行称重查询。作为一个小的优化,我预先计算avg_num_votes * avg_rating并调用它avg_summand

SELECT
  voting.*, -- or whatever fields you need
  ($avg_summand+totalVotes*avgVote)/($avg_num_votes+totalVotes) AS bayesian
FROM voting
ORDER BY  bayesian DESC
LIMIT 1;

编辑

您可以将其作为连接运行:

SELECT
  voting.*, -- or whatever fields you need
  (avg_num_votes*avg_rating+totalVotes*avgVote)/(avg_num_votes+totalVotes) AS bayesian
FROM voting,
(
  SELECT
    SUM(totalVotes)/COUNT(*) AS avg_num_votes,
    SUM(avgVote)/COUNT(*) AS avg_rating
  FROM voting AS iv
) AS avg
ORDER BY  bayesian DESC
LIMIT 1;

但这将计算每个查询的总和和平均值——称之为性能炸弹。

于 2012-11-22T12:30:34.233 回答