0

What I'm trying to achieve is to return a random sample of x size from a dataset, then order it based on a column. This is what I have tried:

SELECT *
FROM Table
WHERE integerField > 0
ORDER BY RAND(), integerField DESC
LIMIT 100

The idea here is that it will first order the table by random, effectively shuffling it, then order the first 100 rows returned by the integerField. I believe the problem is that it does not do the limit before the order, so I'm either going to get 100 random lines back or the first 100 lines of the database ordered by score (In this example, it's the former)

Is there a way to achieve this in a single query, or will the output have to be manually parsed through external logic/additional queries?

Darth
  • 63
  • 11

1 Answers1

0

Solution: Utilise a substring to collect the initial randomised sample, then order it:

(SELECT * FROM Table
WHERE integerString >= 0 
ORDER BY RAND() LIMIT 100) 
ORDER BY integerField DESC

Credit to Akina and jarlh for the pointer to use substring

Darth
  • 63
  • 11