Possible Duplicate:
How to request a random row in SQL?
Because of a huge table I cannot use MySQL function RAND()
and trying to avoid using RAND()
this way:
$a = mysql_fetch_array(mysql_query("SELECT MIN(id), MAX(id) FROM table WHERE category_id='".$var."'"));
$id = rand($a[0],$a[1]);
$id2 = rand($a[0],$a[1]);
Which doesn't work, because: in $a
I have the biggest and smallest ID with the respective category_id
- that's ok.
The problem brings the rows with $id
and $id2
- because there are used the minimal and maximal ID values, but PHP function rand()
will take some random value between minimum and maximum - and there is a big probability, that the picked out record will not belongs into category_id
.
So I would like to ask you about a way, how could I fix this example.
THank you