Jump to content

Archived

This topic is now archived and is closed to further replies.

mcmuney

RAND()

Recommended Posts

I'm using ORDER BY RAND() function, which works. The problem is that when results are > 10 and pagination is used, it randomizes the results on every page. I would like it to randomize once and when viewer goes to page 2, it should not randomize again. How can I achieve this? Thanks.

Share this post


Link to post
Share on other sites
There is no good way of doing that.  The purpose of using RAND() is to get a different sort order each time.

The only thing I can think of off hand would be to do your query, then store the result somewhere...in a session, or in a file, so that when the user goes to the next page you are able to retrieve the same result set and get the next 10 records for them to view.

Share this post


Link to post
Share on other sites
Can you seed the random number that MySQL uses from some known point? If so you could just seed it from the same point twice, but I don't know enough about MySQL to know if you can.

Share this post


Link to post
Share on other sites
According to the manual, you can:

http://mysql.com/doc/refman/5.0/en/mathematical-functions.html#id3220613

Which means that all he would have to do is get a random number, use it so seed mysql's RAND function, then store that number in a session to be used in all subsequent queries.

Good suggestion.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.