大家好,我在 HQL语句中执行 case when 语句出错,难道hibernate 不支持吗??
我的语句是:
// 功能是统计出 2 个总数结果:
// 1. 所有用户的总数
// 2. 所有年龄在20 到 30 之间的用户总数String hql = "select count(*) as count1, count(case when age between 20 and 30 then name end) as count2 from User";
List countList = session.createQuery(hql);
if(countList != null && countList.size()>0){
Object[] countObj = (Object[]) countList.get(0);System.out.println("count1 = "+countObj[0]+" , count2 = "+countObj[1]);
}
但是执行结果报 unexpected token: case 错误。。。。
难道是 Hibernate 不支持这样的 case 语法吗??? 我的 Hibernate 是 3.0 的
问题补充:
奇怪的是我执行sum聚集函数就可以,但为什么 count 函数就不行
String hql = "select count(*) as count1, sum(case when age between 20 and 30 then 1 else 0 end) as count2 from User";