I'm looking to get items from database where the items has options and some not.
My model to get the data is: {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;
But i need to get also items that has no options and options_values.
category_products table:
product_id | category_id | sequence
74 | 2 | 0
75 | 2 | 0
products table:
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
options table:
id | product_id | sequence | name | type | required
74 | 74 | 1 | Size | radiolist | 1
option_values table:
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
With the model i write above i can get only the product_id 74, and my question is how can i adapt the query to get the product_id 75 to?
Any help is appreciated.
Try changing your joins to include LEFT JOIN
. I don't have a database set up so I can't test it but I would try the following:
NOTE : the addition of the left
parameters in the join()
functions.
$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;
You might need to modify the joins to be LEFT
or RIGHT
joins to get the desired result. I may try to simulate it a little later when I get a minute.
This is a good article on understanding MySQL joins. I refer to it from time to time when I have to craft complex queries.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.