Hello,
I am trying to implement something that's similar to those HotOrNot.com & other similar sites i.e. I need to randomly retrieve a row from a large table many many consecutive times.
I've tried something like
SELECT * FROM Table ORDER BY Rand() LIMIT 1
But somehow this doesn't really work. Weirdly, I would often get the same row drawn consecutively ... which isn't too random! So thinking that MySQL's Rand() function doesn't really work too well, I tried another approach.
First, grab the # of rows in the Table ... and then with this $rowTotal ... do something like
mt_srand((double) microtime() * 1000000);
$rand_id = mt_rand(1,$rowTotal);
then ... SELECT * FROM Table WHERE id=$rand_id
A random row would be retrieved. Now, however, if I need to grab a random row with an ADDITIONAL CONDITION, e.g. WHERE name=$name, say
SELECT * FROM Table WHERE name=$name ORDER BY Rand() LIMIT 1
How can i do this in the mt_rand() way???
Or is there any better way? How did those HotOrNot.com site do this similar tasks?
Thank you very much !!!