教条左连接,其中连接表为空显示结果

时间:2016-12-17 05:03:25

标签: symfony doctrine-orm doctrine

此查询的目标是显示尚未创建任何货件的表单中的记录。以下是在SQL中应该如何完成的事情:

SELECT *
FROM orders 
LEFT JOIN orders_shipments shipments ON orders.trx_id = shipments.trx_id
WHERE shipments.shipment_id IS NULL 
AND orders.purchase_date IS NOT NULL 
AND orders.fulfillment_channel = 'MFN';

以下查询显示0结果。以下是:

$qb = $this->createQueryBuilder('orders');
        $qb->select('orders, shipments')
        ->leftjoin('orders.shipments', 'shipments')
        ->Where('shipments.id IS NULL')
        ->ANDWhere('orders.purchaseDate IS NOT NULL')
        ->ANDWhere('orders.fulfillmentChannel = :a')->setParameter('a', 'MFN');;
        $results = $qb->getQuery()
        ->getResult();
        return $results;

是否显示结果。为什么这样以及如何解决它?

1 个答案:

答案 0 :(得分:3)

不知道为什么,但我必须使用GROUP和HAVING让它工作:

$qb = $this->createQueryBuilder('orders');
        $qb->select('orders, shipments')
        ->leftjoin('orders.shipments', 'shipments')
        ->Where('shipments.id IS NULL')
        ->ANDWhere('orders.purchaseDate IS NOT NULL')
        ->ANDWhere('orders.fulfillmentChannel = :a')->setParameter('a', 'MFN')
        ->GroupBy('orders.id')
        ->having('count(shipments) = 0');
        $results = $qb->getQuery()->getResult();