我正在尝试创建一个模型以传递给gsp视图。我想跨两个表做一个子查询。我有两个域,alum_profile和alum_position。 alum_profile有很多alum_position。 alum_position属于alum_profile。在SQL中,如果我想创建一个结果集,我会这样:
Select count(id),
(Select CONCAT(first_name, ' ', last_name)
From alum_profile
where
alum_profile_id =alum_profile.id ) as Person
FROM alum_position
GROUP BY alum_profile_id
ORDER BY count(id) DESC
如何使用HQL执行此操作并创建可传递给gsp View的模型。
感谢您的帮助
杰森
我正在使用Spring Source,使用MySQL并使用grails写入grails
答案 0 :(得分:0)
根据我对您的问题的解读,您希望显示个人资料的名称列表,以及每个个人资料的位置数,按位置数量排序,等等。
首先,您需要模型:
class AlumProfile {
String first_name
String last_name
def hasMany = [positions: AlumPosition]
};
class AlumPosition {
String name // I just added this, no idea what you need in here
def belongsTo=AlumProfile
};
现在您要创建按位置计数排序的AlumProfiles列表。在您的控制器中,您需要:
def allByPositionCount = {
def profiles = AlumProfile.list().sort( [compare: { a,b -> a.positions.size().compareTo( b.positions.size() ) }] as Comparator );
[ profiles: profiles ]
}
这将使allByPositionCount.gsp呈现包含“profiles”成员的模型,该成员是正确顺序的配置文件列表,如下所示:
<g:each in="${profiles}" var="profile" >
${profile.first_name} ${profile.last_name} has ${profiles.positions.size()} positions
</g:each>
应该呈现你想要的东西。