Ferran Gil
Ferran Gil

Reputation: 424

Return random rows from the best ones (i.e: 100 rows -> best 10 -> get 5 random)

Situation: A table with lots of photos, classified and with votes. I'm currently doing this:

SELECT n.nid, n.title, ....... order by v.value desc limit 0,5

What I want is to get a random resultset after sorting my rows by its votes. Now, a table with hundreds of records is giving me the best 5 rows. Always the same 5. This is quite repetitive, but of course giving 5 random rows wouldn't be the best thing, as not all the rows have a good quailty photo. Some might be not too good.

I don't want to just do this:

SELECT n.nid, ...... order by RAND() limit 0,5

What I'm looking for is something like this:

SELECT n.nid, .....   order by RAND( v.value desc limit 0.10) limit 0,5

but of course that is not SQL :) The tables involved have up to 50.000 rows.

Thanks!

Upvotes: 3

Views: 222

Answers (1)

DaClown
DaClown

Reputation: 4569

Wouldn't a nested query do the trick? First select your 10 best photos, then select 5 randomly. Or a temporary table, but it's almost the same a nested query.

Edit: thanks for the link Lukáš

Upvotes: 1

Related Questions