HQL子查询替换

时间:2018-07-04 13:05:31

标签: group-by subquery hql having

如何在HQL中替换此子查询(因为HQL不支持子查询):

SELECT l.aUser, count(l.aUser) 
FROM LifeTable l 
WHERE l.aUser IS NOT NULL
GROUP BY l.aUser 
HAVING count(l.aUser) = 
(SELECT min(cnt) 
 FROM (SELECT count(l.aUser) cnt 
       FROM LifeTable l 
       WHERE l.aUser IS NOT NULL 
       GROUP BY l.aUser) a)

此查询应选择LifeTable中出现最少的aUser。 谢谢!

1 个答案:

答案 0 :(得分:1)

我认为您可以使用all

SELECT l.aUser, count(l.aUser) 
FROM LifeTable l 
WHERE l.aUser IS NOT NULL
GROUP BY l.aUser 
HAVING COUNT(*) <= ALL (SELECT count(*) as cnt 
                        FROM LifeTable l2 
                        WHERE l2.aUser IS NOT NULL AND
                              l2.aUser = l.aUser
                       );

一个略微正确。 HQL确实支持子查询,只是不支持FROM子句。