David19801
David19801

Reputation: 11448

mysql random from top 10

I have the mysql query:

SELECT * FROM bigtable WHERE column1='1' ORDER BY column2 DESC LIMIT 10

And then I put everything in an array and use php to choose a random row from this array of 10 items.

Is there a way to do this with a single mysql query instead of mysql+php part?

Upvotes: 4

Views: 1064

Answers (1)

dani herrera
dani herrera

Reputation: 51675

After take top 10, then take 1 with random:

SELECT * from (
   SELECT * FROM bigtable 
   WHERE column1='1' 
   ORDER BY column2 DESC LIMIT 10
) T ORDER BY RAND() 
LIMIT 1

Upvotes: 7

Related Questions