Order By Rand by Time (HQL)

2019-02-16 02:55发布

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 ?

2条回答
趁早两清
2楼-- · 2019-02-16 03:00

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.

查看更多
SAY GOODBYE
3楼-- · 2019-02-16 03:09

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?

查看更多
登录 后发表回答