• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
山高我为峰
博客园    首页    新随笔    联系   管理    订阅  订阅
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字符

我犯的一个错误:

posted on 2016-01-20 17:42  山高我为峰  阅读(1117)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3