0

我需要实现一个命名查询,它返回一个按参加活动的朋友数量排序的活动列表。

例如,此查询返回按参加活动的人数排序的活动列表:

    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

但是这个查询返回了发生在朋友身上的事件并跳过了其他事件。

这是我的数据库的简化图: 方案

4

1 回答 1

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子句中的子查询,因此您必须再选择一列仅用于排序。

于 2013-04-23T21:54:35.067 回答