选择聚合根,其中所有聚合都满足特定条件

时间:2014-04-16 08:03:28

标签: mysql sql

考虑以下情况,

Order and -item

http://yuml.me/diagram/scruffy;/class/edit/[Order]++-1..*>[OrderItem]

现在我想获取OrderItems行 all 的订单商品,让我们说itemSum > 100左右。或者,对于更具意义的制作用例,可以考虑OrderItem拥有属性shipsFree,并且我想选择每个 OrderItem免费发布的订单。

如果我只是(LEFT) JOIN他们,如果一个OrderItem与条件匹配,则会出现Order,但不会所有

感谢您的想法!

1 个答案:

答案 0 :(得分:0)

案例1:

select
*
from
order o 
inner join orderItem oi on o.id = oi.orderId
group by o.id
having sum(items) > 100

案例2:

select
*
from
order o 
inner join orderItem oi on o.id = oi.orderId
group by o.id
having sum(property = 'shipsFree') = count(*)