HQL 子查询替换
HQL subqueries replacement
如何在 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)
此查询应该select LifeTable 中出现次数最少的用户。
谢谢!
我想你可以使用 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
子句中。
如何在 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)
此查询应该select LifeTable 中出现次数最少的用户。 谢谢!
我想你可以使用 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
子句中。