HQL-COUNT(IF())在编译时给出QuerySyntaxException

时间:2018-11-27 12:43:17

标签: java spring jpa hql

我试图获取状态等于ACCEPTED的AdmissionForms的数量,但此查询未能这样做。

在存储库中,

SELECT COUNT(IF(status='ACCEPTED',1,NULL)), COUNT(IF(status='PENDING',1,NULL)) FROM  admissionforms;

我遇到的异常,

org.hibernate.hql.internal.ast.QuerySyntaxException: expecting CLOSE, found '(' near line 1, column 16 [SELECT COUNT(if(status = 'ACCEPTED',1,NULL)) FROM admissionforms]

1 个答案:

答案 0 :(得分:0)

为什么不只包含WHERE子句呢?

SELECT COUNT(*) AS ACCEPTED 
FROM admissionforms
WHERE status = 'ACCEPTED';

但是,条件聚合也可以这样做:

SELECT SUM(CASE WHEN status = 'ACCEPTED' THEN 1 ELSE 0 END) AS ACCEPTED
FROM admissionforms;