2016-04-13 15:30
浏览 48

Doctrine QueryBuilder HAVING表达式

OK, so I'm rewriting some code, using Doctrine ORM (2.5).

The old code creates a query that has something like this:

SELECT * FROM couples INNER JOIN individuals ON ( = individuals.couple_id)
HAVING SUM(individuals.date_of_birth <= '1976-01-01') > 0

I have no clue how to implement this best using the Doctrine QueryBuilder. This is a very simplified example, the real query is much longer and has a few HAVING clauses, all of which use SUM(some_condition) > 0 to ensure that only Couples that contain a matching Individual will be retrieved.

I can add having clauses in Doctrine using $queryBuilder->having(), but I cannot do so using the SUM() function. Any ideas?

图片转代码服务由CSDN问答提供 功能建议

好的,所以我正在使用Doctrine ORM(2.5)重写一些代码。 \ n


  SELECT * FROM将INNER JOIN个人联系起来( = persons.couple_id)
HAVING SUM(persons.date_of_birth&lt; ='1976-01-01')&gt;  0 

我不知道如何使用Doctrine QueryBuilder实现这一目标。 这是一个非常简化的示例,真正的查询更长,并且有一些HAVING子句,所有这些都使用 SUM(some_condition)&gt; 0 以确保只检索包含匹配个体的Couples。

我可以使用 $ queryBuilder-&gt; having()<在Doctrine中添加having子句 / code>,但我不能使用 SUM()函数。 有什么想法吗?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhi6905 2016-04-13 17:57

    Actually, there is nothing stopping you from using sum together with having:

        ->select('couple, individual')
        ->having('sum(individual.date_of_birth) > 0');

    The sum() function of the query builder, actually takes in two arguments and returns a mathematical expression which is not what you're after.

    When you use sum on a field like in the example above, that's actually the aggregating function.

    Another thing to keep in mind in your case is that, according to the documentation, every having() call overrides all previously set having conditions. So if you want to use multiple of these, use andHaving and orHaving.

    I hope this explains it.

    解决 无用
    打赏 举报

相关推荐 更多相似问题