【问题标题】:Symfony2 Query Builder "SELECT where sum children > 0"Symfony2 查询生成器“SELECT where sum children > 0”
【发布时间】:2016-01-22 05:10:12
【问题描述】:

我正在尝试收集具有子订单 OrderPayments 的订单结果集。我只想拥有 orderpayments.amount 的总和 > 0 的订单。

到目前为止,我有这个但无法让它工作,我得到了一个

未找到列:“where 子句”中的未知列“sclr_0”

    // Create the Query Builder
    $qb = $this->createQueryBuilder('o')->select('o');

    // Joins
    $qb->leftJoin('o.orderPayments', 'op')->addSelect('op')->addSelect('SUM(op.amount) AS total_sum');

    // Criteria
    $qb->where('total_sum > 0');
    $qb->andWhere('o.dateDeleted IS NULL');

    // Return the result
    return $qb->getQuery()->getResult();

【问题讨论】:

  • 你检查日志中的完整查询了吗? @亚当
  • 清除缓存。运行php app/console doctrine:schema:update --force。如果在这些操作之后您仍然有此错误消息,请在此处添加您的实体类代码和 Doctrine 生成的整个 SQL 查询。
  • @AnkiiG 我查了一下,基本上是 SELECT SUM(o.orderPayments) AS sclr_0 与 sclr_0 相关的部分未找到。
  • @Michael 不幸的是,这对我没有帮助,因为我的迁移和差异都是最新的。这是在此处发布之前的明显检查

标签: php mysql symfony query-builder


【解决方案1】:

您是否在数据库中尝试过您的 sql 查询?

您的查询看起来像:

SELECT o.*, SUM(op.amount) AS total 
FROM order o 
LEFT JOIN order_payement op on o.id = op.order_id
WHERE total > 0;

这个查询在mysql上不起作用,你不能在你的WHERE子句中使用SELECT子句的操作结果。

您的查询必须如下所示:

SELECT o.*, SUM(op.amount) AS total 
FROM order o 
LEFT JOIN order_payement op on o.id = op.order_id
HAVING SUM(op.amount) > 0
GROUP BY o.id;

您的查询构建器将是:

$qb = $this->createQueryBuilder('o')
   ->select('o');
   ->leftJoin('o.orderPayments', 'op')
   ->addSelect('op')
   ->addSelect('SUM(op.amount) AS total_sum')
   ->Where('o.dateDeleted IS NULL')
   ->having('SUM(op.amount) > 0')
   ->grouBy('o.id');

【讨论】:

  • 虽然更新后的查询生成器仍然抛出相同的错误,但这是有道理的......
猜你喜欢
  • 2018-12-26
  • 2013-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-03
  • 1970-01-01
  • 2012-10-05
  • 2022-11-15
相关资源
最近更新 更多