PHP MySQL pagination with random ordering

Use RAND(SEED). Quoting docs: “If a constant integer argument N is specified, it is used as the seed value.” (http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand).

In the example above the result order is rand, but it is always the same. You can just change the seed to get a new order.

SELECT * FROM your_table ORDER BY RAND(351);

You can change the seed every time the user hits the first results page and store it in the user session.

Leave a Comment