I have a database of events. Each event has a rank attatched to it (1-5). I want to query the db for events by date range, I want to select a random set of event with rank 1, followed by a random set of of events with rank 2 etc.
This query gives me a random selection of rank 1 events:
SELECT * FROM
(
SELECT * FROM `events`
WHERE `date` >1326578956
AND `date`<1326870399
AND `rank`=1
ORDER BY RAND()
)
e LIMIT 0,10
But there maybe cases where there are no events with rank 1, or rank 2 etc,.
Is this possible with only a MYSQL query. I could iterate the query using PHP, filling an array with results but I think it would be far more elegant to do it all with MYSQL if possible.
Thanks,
Alan.