PHP MySQL pagination with random ordering

后端 未结 6 1597
生来不讨喜
生来不讨喜 2020-12-05 11:25

This is a problem with a ordering search results on my website,

When a search is made, random results appear on the content page, this page includes pagination too.

6条回答
  •  囚心锁ツ
    2020-12-05 11:31

    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.

提交回复
热议问题