简体   繁体   English

如何显示随机博客帖子,支持高收视率的帖子?

[英]How do I display random blog posts, favoring posts with high ratings?

Let's say I have a Blog posts table that has a rating field which indicates the quality of the post. 假设我有一个博客帖子表,其中有一个评级字段,表示帖子的质量。 What is the most efficient way to randomly find a post, with a higher chance of returning a highly ranked post? 随机查找帖子的最有效方法是什么,返回高排名帖子的可能性更高?

I will be implementing this in PHP, MySQL, and possibly Lucene. 我将在PHP,MySQL和Lucene中实现它。

An easy solution would be to include calls to RAND() and to the rating column and multiplying them together: 一个简单的解决方案是包括对RAND()rating列的调用并将它们相乘:

SELECT title, content FROM blog_posts ORDER BY (rating + 1) * RAND() DESC LIMIT 1;

If you found this gave too much precedence to items with a high rating, you could use SQRT : 如果您发现这对具有高评级的项目给予太多优先权,则可以使用SQRT

SELECT title, content FROM blog_posts ORDER BY SQRT(rating + 1) * RAND() DESC LIMIT 1;

You could use a "weighted random" ordering, such as: 您可以使用“加权随机”排序,例如:

SELECT title, body FROM posts ORDER BY (score+1) * RAND() DESC LIMIT 5

The +1 is there to allow posts with 0 score to be selected. +1是允许选择0分的帖子。 Depending on the average score of your post you may have to multiply the score by another constant factor (eg 0.5*score+1 ) . 根据您帖子的平均分数,您可能需要将分数乘以另一个常数因子(例如0.5*score+1

Depending on the distribution of your scores you may want to transform your score, for instance with LOG(score) or SQRT(score) . 根据您的分数分布,您可能希望转换您的分数,例如使用LOG(score)SQRT(score)

SQL COMMAND : SQL命令:

SELECT * FROM posts WHERE id = '$randomly_generated_ids' ORDER BY ratings ASC

for example $randomly_generated_ids can be "13,10,5,20" 例如$ random_generated_ids可以是“13,10,5,20”

Details for : ORDER BY 详细信息: ORDER BY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM