Order By Rand by Time (HQL)

谁说我不能喝 提交于 2019-12-30 07:54:46

问题


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()";

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 ?


回答1:


Seed the random number generator:

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.




回答2:


I don't see why you're deliberately randomising the data. If there's no particular order you want them in, why not just order by the primary key?



来源:https://stackoverflow.com/questions/2915333/order-by-rand-by-time-hql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!