我需要实现一个命名查询,该查询返回按参加活动的朋友数排序的事件列表。
例如,此查询返回按进入事件的所有人的数量排序的事件列表:
SELECT r.event
FROM RSVP AS r
JOIN r.event.rsvpList rr
WHERE r.profile.user.uuid = :userUuid
AND r.rsvpStatus = RSVPStatus.ATTENDING
GROUP BY r.event
ORDER BY COUNT(rr) DESC
或其他示例,朋友事件列表:
SELECT r.event
FROM RSVP AS r
WHERE r.profile IN
( SELECT f.profileTo
FROM Friend f
WHERE f.profileFrom.user.uuid = :userUuid
)
AND r.rsvpStatus = :rsvpStatus
GROUP BY r.event
我尝试执行以下操作:
SELECT r.event
FROM RSVP AS r
JOIN r.event.rsvpList rr
WHERE r.profile.user.uuid = :userUuid
AND r.rsvpStatus = RSVPStatus.ATTENDING
AND rr.profile IN
(
SELECT f.profileTo
FROM Friend f
WHERE f.profileFrom.user.uuid = :userUuid
)
GROUP BY r.event
ORDER BY COUNT(rr) DESC
但是此查询返回了发送给朋友的事件并跳过其他事件。
这是我的数据库的简化图:
答案 0 :(得分:1)
您没有发布您的实体,所以我无法尝试,但我认为这应该有效:
SELECT r.event, (
SELECT count(*)
FROM RSVP rr
WHERE rr.event = r.event
AND rr.profile IN
(
SELECT f.profileTo
FROM Friend f
WHERE f.profileFrom.user.uuid = :userUuid
)
) count
FROM RSVP AS r
WHERE r.profile.user.uuid = :userUuid
AND r.rsvpStatus = RSVPStatus.ATTENDING
ORDER BY 2 DESC
这里的问题是HQL不支持ORDER BY
子句中的子查询,因此您只需再选择一列进行排序。