HQL查询中取个别几个字段

数据表:

要求:查询type为4001,以content分组,本计算不重复的pcInfoId

MySQL语句:

SELECT COUNT(DISTINCT pcInfoId) AS COUNT,content,TIME FROM vrv_paw_reportlog WHERE TYPE='4001' GROUP BY content

对应的HQL语句:

 SELECT  new map(COUNT(distinct this.pcInfo.id) AS count,this.content as content,this.time as time) 
 From com.vrv.paw.domain.ReportLog this
 Where this.type=4001
 Group by this.content

注:

使用关键字as给“被选择了的表达式”指派别名:

select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n
from Cat cat

这种做法在与子句select new map一起使用时最有用:

select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n )
from Cat cat

该查询返回了一个Map的对象,内容是别名与被选择的值组成的名-值映射

这样可以方便产生JSON字符

我犯的一个错误:

原文地址:https://www.cnblogs.com/liaojie970/p/5146011.html