问题
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