带distinct的hql查询分页时,不能使用select count(*) from (hql) 这样的方式,这样获得的总数是不对的。
这里的做法是分离出hql的select dinstinc和from之间的部分,变成 select count(distinct … ) from…
代码如下:
/**
* select dinstinct 查询的总数
* 这里按 select count(distinct … ) from … 获得总数.
*
* @param hql
* @param values
* @return
*/
private long getSelectDistinctCount(String hql, Object… values) {
int distinctIndex = hql.toLowerCase().indexOf("distinct");
int fromIndex = hql.toLowerCase().indexOf("from");
hql = removeOrders(hql);
String countQueryString = " select count(" + hql.substring(distinctIndex,fromIndex) + ") " + hql.substring(fromIndex);
List countlist = this.find(countQueryString, values);
long totalCount = (Long) countlist.get(0);
* select dinstinct 查询的总数
* 这里按 select count(distinct … ) from … 获得总数.
*
* @param hql
* @param values
* @return
*/
private long getSelectDistinctCount(String hql, Object… values) {
int distinctIndex = hql.toLowerCase().indexOf("distinct");
int fromIndex = hql.toLowerCase().indexOf("from");
hql = removeOrders(hql);
String countQueryString = " select count(" + hql.substring(distinctIndex,fromIndex) + ") " + hql.substring(fromIndex);
List countlist = this.find(countQueryString, values);
long totalCount = (Long) countlist.get(0);
if (totalCount < 1) {
return 0;
} else
return totalCount;
}
相关推荐
hibernate HQL详细教程,简单易学易懂 1.1 HQL基础 2 1.1.1 默认数据库表和数据 2 1.1.2 检索类的所有对象 4 1.1.3 检索类的某几个属性 5 1.1.4 指定别名 5 1.1.5 where条件子句 5 1.1.6 使用distinct过滤掉重复值 6...
使用Distinct查询.rar使用Distinct查询.rar
EFCore查询不重复数据Distinct,根据两个字段排序
3.统计函数查询: 1: count() 统计记录的条数 2: min() 求最小值 3: max() 求最大值 4: sum() 求和 4: avg() 求平均值 //取得Student的数量 Query query=session.createQuery("select count(*) from ...
有这样的一个需求:select count(distinct nick) from user_access_xx_xx; 这条sql用于统计用户访问的uv,由于单表的数据量在10G以上,即使在user_access_xx_xx上加上nick的索引, 通过查看执行计划,也为全索引扫描...
MySQL通常使用GROUPBY(本质上是排序动作)完成DISTINCT操作,如果DISTINCT操作和ORDERBY操作组合使用,通常会用到临时表.这样会影响性能. 在一些情况下,MySQL可以使用索引优化DISTINCT操作,但需要活学活用.本文涉及一个...
mysql中distinct用法【SQL中distinct的用法】.docx
完美解决distinct中使用多个字段的方法,完美解决distinct中使用多个字段的方法完美解决distinct中使用多个字段的方法完美解决distinct中使用多个字段的方法完美解决distinct中使用多个字段的方法
用Distinct在MySQL中查询多条不重复记录值,绝对的物有所值
mysql中利用select语句的一个特性就可以很方便地实现查询结果的分页,select语句实现mysql分页查询的语法: 代码如下: SELECT [STRAIGHT_JOIN] [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [HIGH_PRIORITY] [DISTINCT | ...
sql中select语句中的distinct的用法
oracle rownum和distinct
含义:distinct用来查询不重复记录的条数,即distinct来返回不重复字段的条数(count(distinct id)),其原因是distinct只能返回他的目标字段,而无法返回其他字段 用法注意: 1.distinct【查询字段】,必须放在要查询...
主要介绍了MongoDB教程之聚合,MongoDB除了基本的查询功能之外,还提供了强大的聚合功能,这里主要介绍count、distinct和group,需要的朋友可以参考下
在 MySQL 查询中,可能会包含重复值。这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值。 关键词 DISTINCT 用于返回唯一不同的值,就是去重啦。用法也很简单: SELECT DISTINCT * FROM tableName ...
distinct的使用.docx
在使用MySQL时,有时需要查询出某个字段不重复的记录,这时可以使用mysql提供的distinct这个关键字来过滤重复的记录,但是实际中我们往往用distinct来返回不重复字段的条件(count(distinct id)),其原因是distinct...
Oracle分页存储过程,根据指定条件分页 并附上使用示例(C#代码) 注:不能查询视图或者包含distinct、group by、inner join的子句
看过此博文后Oracle、MySql、SQLServer 数据分页查询,在根据公司的RegionRes表格做出了 SQLserver的分页查询语句: SELECT DISTINCT TOP 500 rr.ID AS id ,rr.strName AS name ,rr.nType AS res_type ,...