CP1985
CP1985

Reputation: 277

MYSQL Random Row/Ordering question

I am trying to select the three rows that have the highest scores. Obviously I can use a simple things such as:

SELECT * FROM myTable ORDER BY myscore DESC LIMIT 3;

But what I also want is an easy way for this to return only one of these three rows randomly. Normally I just do an ORDER BY rand(), but what is the cleanest way to add the constraint that it has to be a random one from only the top 3 scored rows?

Thanks!

Upvotes: 2

Views: 110

Answers (1)

Martin Smith
Martin Smith

Reputation: 454020

SELECT * FROM
(
SELECT * FROM myTable ORDER BY myscore DESC LIMIT 3
) Top3
ORDER BY RAND() LIMIT 1;

Upvotes: 4

Related Questions