This is a problem with a ordering search results on my website,
When a search is made, random results appear on the content page, this page includes pagination too.
The combination of
is as ugly as it comes: 1. and 2. together need some sort of "persistent randomness", while 3. makes this harder to achieve. On top of this 1. is not a job a RDBMS is optimized to do.
My suggestion depends on how big your dataset is:
Few rows (ca. <1K):
Many rows (10K+):
This assumes, you have an AUTO_INCREMENT unique key called ID with a manageable number of holes. Use a amintenace script if needed (high delete ratio)
rand_id(continuous_id)$a=array(rand_id(100,000), rand_id(100,001), ... rand_id(100,009));$a=implode(',',$a);$sql="SELECT foo FROM bar WHERE ID IN($a) ORDER BY FIELD(ID,$a)";