【发布时间】: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