Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zend framework join 3 tables

I have 3 tables (order, product, order_item). In order i have the date. In order_item i have product_id and order_id. I need to select all products with orders, what created in the current month. It is my select:

$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 haven't orders result is empty. And i should always have all products. Sorry for my english. Thanks.

like image 778
Alex Pliutau Avatar asked Feb 16 '26 17:02

Alex Pliutau


1 Answers

It was very hard. The right 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
like image 145
Alex Pliutau Avatar answered Feb 19 '26 05:02

Alex Pliutau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!