如何在CakePHP中查询NOT查询?

时间:2016-12-23 05:09:09

标签: php cakephp

我创建了一个运行完美的SQL子查询。

SELECT o.*, u.*,OD.* FROM orders o
LEFT JOIN order_details OD on o.id = OD.order_id
LEFT JOIN users u ON o.user_id = u.id 
WHERE o.order_status = 1 AND o.id NOT IN 
   (SELECT r.order_id FROM order_rejected_details r WHERE r.courier_id =$courierId) 
order by o.id desc

如何在CakePHP的条件下用NOT编写上述查询?

1 个答案:

答案 0 :(得分:-2)

您可以在cakephp中简单地编写此查询,如下所示: 在模型中:

$resultData = $this->query('SELECT o.*, 
       u.*, 
       OD.* 
FROM   orders o 
       LEFT JOIN order_details OD 
              ON o.id = OD.order_id 
       LEFT JOIN users u 
              ON o.user_id = u.id 
WHERE  o.order_status = 1 
       AND o.id NOT IN (SELECT r.order_id 
                        FROM   order_rejected_details r 
                        WHERE  r.courier_id = $courierid) 
ORDER  BY o.id DESC');

return $resultData;

试试这个,希望这可以帮到你。

感谢。