选择最大计数HQL

时间:2016-06-17 08:06:37

标签: sql hibernate hql

请任何人帮我转换为HQL吗?

 SELECT COUNT(customerId) AS `cnt` 
 FROM property
 GROUP BY customerId 
 ORDER BY `cnt` 
 DESC LIMIT 1

1 个答案:

答案 0 :(得分:2)

您的SQL查询:

SELECT COUNT(customerId) AS `cnt`
FROM property GROUP BY customerId ORDER BY `cnt` DESC LIMIT 1

可以在HQL中以相​​同的方式编写。

重要的是:你如何映射POJO。

所以我想你有一个类属性,你有一个属性customerId。

所以你的查询可能是:

SELECT COUNT(P.customerId) AS `cnt`
FROM Property P
GROUP BY P.customerId
ORDER BY `cnt` DESC LIMIT 1

最后,我建议您以这种方式重写SQL查询以获取MAX值:

SELECT MAX(t.cnt)
FROM 
    (SELECT COUNT(customerId) as cnt
    FROM property) t