本来想要效果是有多少个userId,结果却显示的是,去重后每组userId有多少个。
select count(id) from ls_wweb_log_call where userId is not null and companyUserId=:companyUserId and createDate BETWEEN :nowDate AND :nextDate group by userId
后来使用如下方式解决:
select count(id) from (select id from ls_wweb_log_call where userId is not null and companyUserId=:companyUserId and createDate BETWEEN :nowDate AND :nextDate group by userId) temp
达到想要的效果,注意,这里在hibernate中需要使用createSQLQuery,然后query.uniqueResult()返回的也不是Long而是java.math.BigInteger,如需要转long,直接用longValue()即可。
本文讨论了如何在SQL查询中正确获取去重后的userId数量,通过使用子查询和特定的Hibernate方法来实现目标效果。重点介绍了如何在hibernate中使用createSQLQuery和query.uniqueResult()来解决需求,特别注意到了返回类型为java.math.BigInteger而非Long,以及如何进行转换。
993

被折叠的 条评论
为什么被折叠?



