检索相关记录数量最少的记录
Property 和Reservation 之间存在一对二多的关系。酒店有名为“nr_of_bookings”的列。我需要 DQL 来仅检索 COUNT(Reservation) 小于 Property.nr_of_bookins 的属性。
示例:某些房产的 nr_of_bookings = 3 。如果它有 5 个相关预订,则不会被检索。但如果它有 2 个相关的预订,则会将其检索。
我尝试了多种组合,但我显然错过了一些东西。我发布了类似的问题 此处,但最好从头开始。谢谢。
There is one-2-many relation between Property and Reservation. Property has column called 'nr_of_bookings'. I need DQL that will retrieve only the properties that has COUNT(Reservation) less than Property.nr_of_bookins.
An example: Some Property has nr_of_bookings = 3 . If it has 5 Reservations related, it will not be retrieved. But if it has 2 related Reservations, it will be retrieved.
I tried numerous combinations, but I miss something obviosly. I posted similar question
here , but it is better to start from scratch. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过:
编辑:以上适用于 Doctrine 2。对于 Doctrine 1.2,查看您的代码,我猜您的
HAVING
子句引用的内容不在 group by 或 a 的结果中聚合函数。尝试如下操作:您按
p.id
进行分组,因此SUM(p.nr_of_bookings)
将等于p.nr_bookings
。来自 MySQL 文档:
Have you tried:
EDIT: The above is for Doctrine 2. For Doctrine 1.2, looking at your code, I'm guessing your
HAVING
clause is referencing something that's not in a group by or the result of an aggregate function. Try something like this:You're grouping by
p.id
soSUM(p.nr_of_bookings)
will be equal top.nr_bookings
.From the MySQL documentation: