LIMIT, then RAND, not RAND, then LIMIT

I use full-text search to stretch strings.
I order rows based on the score (ORDER BY SCORE), and then from the top 20 rows (LIMIT 20), I want a rand (RAND) result.

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

My workaround is based on code where I put the top 20 in an array and then randomly select 5.

Is there a sql way for this?

+3
source share
1 answer

, . . :

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

Source: https://habr.com/ru/post/1741333/


All Articles