zend框架联接3个表 [英] zend framework join 3 tables

查看:80
本文介绍了zend框架联接3个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个表(order,product,order_item).在order中,我有日期.在order_item中,我有product_idorder_id.我需要选择所有带有订单的产品,即当月创建的产品.这是我的选择:

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.

推荐答案

这很难.正确的SQL:

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

这篇关于zend框架联接3个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆