按时间排序 (HQL) [英] Order By Rand by Time (HQL)
问题描述
我正在使用 asp.net Mvc 2 和 NHibernate 开发 Web 应用程序,并且我正在我的页面中分页数据(类别中的产品),但这些数据是随机的,因此,我使用的是 HQL 语句链接:
I'm developing a web application using asp.net Mvc 2 and NHibernate, and I'm paging data (products in a category) in my page, but this data are random, so, I'm using a HQL statement link this:
string hql = "from Product p where p.Category.Id=:IdCategory order by rand()";
它工作正常,但是当我翻页时,有时相同的产品会出现在第一页、第二页等...页面中,因为它是按 rand() 排序的.
It's working fine, but when I page, sometimes the same product appears in the first, second, etc... pages because it's order by rand().
有没有办法按周期(内部时间)固定随机顺序?或者有什么解决办法?
Is there any way to make a random order by fixed by period (time internal) ? Or any solution ?
推荐答案
种子随机数生成器:
order by rand(123)
我建议使用会话范围的随机数作为种子.这样一来,页面就不会突然为单个用户重新排序,但仍会针对每个用户进行不同的排序.
I would suggest using a session-scoped random number as your seed. That way, the page doesn't suddenly re-sort for a single user, but it's still be sorted differently for every user.
这篇关于按时间排序 (HQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!