Zend framework joins 3 tables

I have 3 tables (order, product, order_item). As orderI have a date. In order_item, I have product_idand order_id. I need to select all products with orders created in the current month. It is my choice:

$select = $this->select()
    ->setIntegrityCheck(false)
    ->from(array('o' => 'order'))
    ->join(array('oi' => 'order_item'), 'o.id = oi.order_id', array('quantity'))
        ->joinLeft(array('p' => 'product'), 'p.id = oi.product_id', array('id', 'pbv', 'name'))
        ->where('MONTH(o.date) = MONTH(CURDATE())');

But when I have no orders, the result is empty. And I always have to have all the products. Sorry for my English. Thank.

+3
source share
2 answers

It was very difficult. Correct SQL:

USE lyf;
SELECT
  *
FROM
  `order` AS o
  LEFT JOIN order_item AS oi ON oi.order_id = o.id
  RIGHT JOIN product AS p ON oi.product_id = p.id
WHERE
  IF(o.`date` IS NOT NULL, MONTH(o.`date`) = MONTH(NOW()), 1) = 1
+1
source

You need to either switch joinLeft to joinRight, or first put your product table in the query.

0
source

Source: https://habr.com/ru/post/1786324/


All Articles