LIMIT,然后RAND,而不是RAND,然后LIMIT [英] LIMIT then RAND rather than RAND then LIMIT

查看:84
本文介绍了LIMIT,然后RAND,而不是RAND,然后LIMIT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用全文搜索来拉行.
我根据得分(ORDER BY SCORE)对行进行排序,然后在前20行(LIMIT 20)中,对结果集进行rand(RAND).

I'm using full text search to pull rows.
I order the rows based on score (ORDER BY SCORE) , then of the top 20 rows (LIMIT 20), I want to rand (RAND) the result set.

因此,对于任何特定的搜索词,我想随机显示前20个结果中的5个.

So for any specific search term, I want to randomly show 5 of the top 20 results.

我的解决方法是基于代码的-我将前20个放入数组中,然后随机选择5.

My workaround is code based- where I put the top 20 into an array then randomly select 5.

是否有SQL方法可以做到这一点?

Is there sql way to do this?

推荐答案

您可以使用内部选择来执行此操作.在内部选择中选择前二十行.在外部选择顺序中,随机选择以下行,然后选择前五名:

You can do this using an inner select. Select the top twenty rows in the inner select. In the outer select order these rows randomly and select the top five:

SELECT *
FROM (
    SELECT *
    FROM table1
    ORDER BY score DESC
    LIMIT 20
) AS T1
ORDER BY RAND()
LIMIT 5

这篇关于LIMIT,然后RAND,而不是RAND,然后LIMIT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆