HQL Min Max,如何

时间:2010-03-04 15:27:13

标签: hibernate hql

我有两个对象:“母亲”和“孩子”。 妈妈有很多孩子, 我如何从DB中得到一个只有2个孩子(或更少),更年轻和更年长的母亲。 感谢

编辑:

母亲我想要有很多孩子,但我只想要年轻人和年长者。

类似的东西:

从妈妈那里离开加入m.Child c 其中(max(c.age)或min(c.age))

2 个答案:

答案 0 :(得分:1)

FROM Mother m WHERE count(m.Children) <= 2

答案 1 :(得分:1)

我找到了这个并且有效:

from M as m    
left join m.C as c
where m.Id = :idM
and
(c.Age = (select min(c.Age) from C c where c.M.Id = :idM)
or
c.Age = (select max(c.Age) from C c where c.M.Id = :idM))
order by c.Age
相关问题