I have a table of product. I have listed the items with pagination using rand() function. When using rand(), it displays the items in random order but the record is repeated while we go from 1 page to another page. Is it possible to get non repeated items in each page using rand()? If yes how, if no what may be the option for this.
我有一张产品表。我使用rand()函数列出了具有分页的项目。当使用rand()时,它以随机顺序显示项目,但是当我们从1页面转到另一页面时,记录会重复。是否可以使用rand()在每个页面中获取非重复项?如果是,如果没有可能是什么选项。
3 个解决方案
#1
In a situation like that, I might see if there's a way to create a list of the product IDs (I'm assuming each product has some sort of unique ID) and shuffle it. The list might be stored as a temporary database table or it might be stored as a session variable in your web application, for example... that way you could get access to any part of the randomly ordered list without having to recompute the whole thing from the beginning.
在这种情况下,我可能会看到是否有办法创建产品ID列表(我假设每个产品都有某种独特的ID)并将其洗牌。该列表可能存储为临时数据库表,也可能存储为Web应用程序中的会话变量,例如......这样您就可以访问随机排序列表的任何部分而无需重新计算整个事物从最开始。
This is obviously best if you have few people accessing the site at any given time, but you expect each of them to go through several pages of the results.
如果您在任何给定时间访问该网站的人很少,这显然是最好的,但您希望每个人都能浏览几页结果。
#2
Well, you could generate a random seed when doing the first query (in your app code), and then keep that seed between requests - and supply this seed to the rand([seed])
function. That should do it...
好吧,您可以在执行第一个查询时(在您的应用程序代码中)生成随机种子,然后在请求之间保留该种子 - 并将此种子提供给rand([seed])函数。应该这样做......
Of course, the specifics would depend on how exactly your sql code is working at the moment; you might need to loop to comsume the first page (or so) of random numbers to ignore.
当然,细节取决于你的sql代码目前是如何工作的;您可能需要循环以使用随机数的第一页(或左右)来忽略。
#3
SELECT column FROM table ORDER BY RAND() LIMIT 1
SELECT列FROM表ORDER BY RAND()LIMIT 1
#1
In a situation like that, I might see if there's a way to create a list of the product IDs (I'm assuming each product has some sort of unique ID) and shuffle it. The list might be stored as a temporary database table or it might be stored as a session variable in your web application, for example... that way you could get access to any part of the randomly ordered list without having to recompute the whole thing from the beginning.
在这种情况下,我可能会看到是否有办法创建产品ID列表(我假设每个产品都有某种独特的ID)并将其洗牌。该列表可能存储为临时数据库表,也可能存储为Web应用程序中的会话变量,例如......这样您就可以访问随机排序列表的任何部分而无需重新计算整个事物从最开始。
This is obviously best if you have few people accessing the site at any given time, but you expect each of them to go through several pages of the results.
如果您在任何给定时间访问该网站的人很少,这显然是最好的,但您希望每个人都能浏览几页结果。
#2
Well, you could generate a random seed when doing the first query (in your app code), and then keep that seed between requests - and supply this seed to the rand([seed])
function. That should do it...
好吧,您可以在执行第一个查询时(在您的应用程序代码中)生成随机种子,然后在请求之间保留该种子 - 并将此种子提供给rand([seed])函数。应该这样做......
Of course, the specifics would depend on how exactly your sql code is working at the moment; you might need to loop to comsume the first page (or so) of random numbers to ignore.
当然,细节取决于你的sql代码目前是如何工作的;您可能需要循环以使用随机数的第一页(或左右)来忽略。
#3
SELECT column FROM table ORDER BY RAND() LIMIT 1
SELECT列FROM表ORDER BY RAND()LIMIT 1