我有PHP查询来显示MYSQL数据库的结果。我使用Swiper jquery插件在每个幻灯片页面上显示9个结果(有3个幻灯片)。然后,当有人进入下一页时,它会显示下一页9.这是我用来完成此任务的PHP查询。
$query_page_1 = mysqli_query( $connection, "SELECT * FROM tblclients WHERE tblclients.package = 'standard' ORDER BY RAND() LIMIT 0, 9");
$query_page_2 = mysqli_query( $connection, "SELECT * FROM tblclients WHERE tblclients.package = 'standard' ORDER BY RAND() LIMIT 9, 9");
$query_page_3 = mysqli_query( $connection, "SELECT * FROM tblclients WHERE tblclients.package = 'standard' ORDER BY RAND() LIMIT 18, 9");
我的问题是,当我使用ORDER BY RAND()随机化每个页面上的结果时,它将从第1页到第2页重复一些相同的结果。
答案 0 :(得分:0)
ORDER BY RAND()
在整个表数据集中随机化行。Rand()
对此结果进行排序,以便随机化这些结果。查询可能如下所示:
SELECT
*
FROM
(
SELECT
*
FROM tblclients
WHERE tblclients.package = 'standard'
ORDER BY tblclients.Primary_Key
LIMIT 0, 9
) AS t
ORDER BY RAND()
在你的情况下:
$query_page_1 = mysqli_query( $connection, "SELECT * FROM ( SELECT * FROM tblclients ORDER BY tbclients.Primary_Key LIMIT 0, 9 ) t ORDER BY RAND()");
$query_page_2 = mysqli_query( $connection, "SELECT * FROM ( SELECT * FROM tblclients ORDER BY tbclients.Primary_Key LIMIT 9, 9 ) t ORDER BY RAND()");
$query_page_3 = mysqli_query( $connection, "SELECT * FROM ( SELECT * FROM tblclients ORDER BY tbclients.Primary_Key LIMIT 18, 9 ) t ORDER BY RAND()");
注意:您需要用tbclients.Primary_Key
替换Primary key
。
其他两个查询也是如此。
答案 1 :(得分:0)
要解决此问题,您需要在整个页面中提供公共种子。例如:
productName
42 是常见的随机种子。