Is there any built-in function except order by random() to select a random record in mysql table?
A:
I don't think so... why don't you want to use the one that works?
BTW, I'm pretty sure it is ORDER BY RAND()
.
I've read it can be a performance problem on many rows... do you have many rows?
alex
2010-06-11 04:49:44
no,i have about 1000 tips but i have around 10000 users, it will slow down the process and also the tips are based on category.
RSGanesh
2010-06-15 04:55:39
+1
A:
No, but you can make it into two questions. In pseudo-PHP-and-MySQL-code:
$max = SELECT COUNT(*) FROM example;
$rand = rand(0, $max-1);
SELECT * FROM example LIMIT $rand, 1;
The right way would probably be to make it into a stored procedure.
Emil Vikström
2010-06-11 04:50:01
Thanks for your immediate response. Actually i dont want to run two select queries as i am using this for displaying tips for user randomly. Is there any other idea?
RSGanesh
2010-06-11 04:59:27
@rshivaganesh - this method has *much* better performance than `order by rand()`
nickf
2010-06-11 05:10:33
Not that I'm aware of. I would go with ORDER BY RAND() on a small table. Maybe you can cache the result from the first query in some way?
Emil Vikström
2010-06-11 05:11:20