【问题标题】:select with multiple join and conditions where column null codeigniter选择多个连接和条件,其中列为空 codeigniter
【发布时间】:2015-01-12 18:06:37
【问题描述】:

我希望从数据库中获取项目,其中项目有选项,有些没有。

我获取数据的模型是:{This return only products that product.id is in options and option_values}

$category_id = 2;
$this->db->select('category_products.*, products.*, option_values.price as prodPrice, option_values.special_price, LEAST(IFNULL(NULLIF(option_values.special_price, 0), option_values.price), option_values.price) as sort_price', false)
         ->from('category_products')
         ->join('products', 'category_products.product_id=products.id')
         ->join('options', 'options.product_id=attributes.product_id')
         ->join('option_values', 'option_values.option_id=options.id')
         ->where('category_products.category_id', $category_id)
         ->where('option_values.inventory >', '0');
         ->where('products.quantity >', '0');
$this->db->group_by('products.id');

$result = $this->db->get()->result();
return $result;

但我还需要获取没有选项和选项值的项目。

category_products 表:

product_id | category_id | sequence
74 | 2 | 0
75 | 2 | 0

产品表:

id | code | name | type | price | saleprice | quantity
74 | 12345_ | Product with options | 1 | 0 | NULL | 1
75 | 12346_ | Product without options | 2 | 199 | NULL | 1

选项表:

id | product_id | sequence | name | type | required
74 | 74 | 1 | Size | radiolist | 1

option_values 表:

id | option_id | name | value | price | special_price | weight | inventory | sequence | limit
777 | 74 | 8K | 12345 | 199.00 | 159.00 | 1.00 | 0 | 17 | NUL

使用我上面写的模型,我只能得到 product_id 74,我的问题是如何调整查询以获取 product_id 75?

感谢任何帮助。

【问题讨论】:

  • 做左连接而不是内连接。->join('products', 'category_products.product_id=products.id',"left")

标签: php codeigniter


【解决方案1】:

尝试更改您的联接以包含LEFT JOIN。我没有设置数据库,因此无法对其进行测试,但我会尝试以下操作:

注意:在join() 函数中添加left 参数。

$category_id = 2;
            $this->db->select('category_products.*, products.*, option_values.price as prodPrice, option_values.special_price, LEAST(IFNULL(NULLIF(option_values.special_price, 0), option_values.price), option_values.price) as sort_price', false)
                    ->from('category_products')
                    ->join('products', 'category_products.product_id=products.id', 'left')
                    ->join('options', 'options.product_id=attributes.product_id', 'left')
                    ->join('option_values', 'option_values.option_id=options.id', 'left')
                    ->where('category_products.category_id', $category_id)
                    ->where('option_values.inventory >', '0');
                    ->where('products.quantity >', '0');
            $this->db->group_by('products.id');

            $result = $this->db->get()->result();

            return $result;

您可能需要将连接修改为 LEFTRIGHT 连接以获得所需的结果。等我一分钟后,我可能会尝试模拟它。

这是一个 good article 了解 MySQL 连接。当我必须制作复杂的查询时,我会不时参考它。

【讨论】:

  • 是的,只有左连接有效,我删除 ->where('option_values.inventory >', '0');因为对于列库存中没有 option_values 的产品为空。
猜你喜欢
  • 2021-07-01
  • 2018-05-28
  • 1970-01-01
  • 1970-01-01
  • 2018-07-23
  • 2020-09-08
  • 1970-01-01
  • 2020-11-05
  • 1970-01-01
相关资源
最近更新 更多