HQL分組查詢

HQL查詢語句使用group by 子句進行分組查詢,使用having子句篩選分組結(jié)果。

按職位統(tǒng)計員工個數(shù)

 Long count=(Long)this.getCurrentSession().createQuery("select count(e.id) from Emp  e group by e.job").uniqueResult();

Hibernate:
select
count(emp0_.empNo) as col_0_0_
from
project.Emp emp0_
group by
emp0_.job

統(tǒng)計各個部門的平均工資

List<Object[]> avg=this.getCurrentSession().createQuery("select e.department.deptName,avg (e.salary) from Emp e group by e.department.deptName").list();

Hibernate:
select
department1_.deptName as col_0_0_,
avg(emp0_.salary) as col_1_0_
from
project.Emp emp0_,
project.Department department1_
where
emp0_.deptNo=department1_.deptNo
group by
department1_.deptName

統(tǒng)計各個職位的最低工資和最高工資

   List<Object[]> maxMin=this.getCurrentSession().createQuery("select e.job,max (e.salary),min (e.salary) from Emp e group by e.job").list();

Hibernate:
select
emp0_.job as col_0_0_,
max(emp0_.salary) as col_1_0_,
min(emp0_.salary) as col_2_0_
from
project.Emp emp0_
group by
emp0_.job

統(tǒng)計平均工資高于4000元的部門名稱,輸出部門名稱,部門平均工資

 List<Object[]> avg=this.getCurrentSession().createQuery("select e.department.deptName,avg (e.salary) from Emp e group by e.department.deptName having avg (e.salary)>4000").list();

Hibernate:
select
department1_.deptName as col_0_0_,
avg(emp0_.salary) as col_1_0_
from
project.Emp emp0_,
project.Department department1_
where
emp0_.deptNo=department1_.deptNo
group by
department1_.deptName
having
avg(emp0_.salary)>4000

使用select子句時,Hibernate返回的查詢結(jié)果為關(guān)系數(shù)據(jù)而不是持久化對象,不會占用Session緩存。為了方便訪問,可以定義一個JavaBean進行投影查詢來封裝查詢結(jié)果中的關(guān)系數(shù)據(jù)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 引出 ?請思考如下問題? –查詢所有員工的每個月工資總和,平均工資? –查詢工資最高和最低的工資是多少? –查詢公...
    C_cole閱讀 7,392評論 0 3
  • 5.多表查詢 多表查詢 目的:從多張表獲取數(shù)據(jù) 前提:進行連接的多張表中有共同的列 等連接 通過兩個表具有相同意義...
    喬震閱讀 1,549評論 0 0
  • ORACLE自學(xué)教程 --create tabletestone ( id number, --序號usernam...
    落葉寂聊閱讀 1,252評論 0 0
  • 查詢是數(shù)據(jù)的一個重要操作。用戶發(fā)送查詢請求,經(jīng)編譯軟件變異成二進制文件供服務(wù)器查詢,后返回查詢結(jié)果集給用戶,查詢會...
    產(chǎn)品小正閱讀 1,535評論 0 2
  • oracle 數(shù)據(jù)庫的scott帳號。 <>作為查詢條件時,可以使用!= 來替換。 SQL> select * f...
    莊棟棟閱讀 2,622評論 0 0

友情鏈接更多精彩內(nèi)容