简体   繁体   中英

Fetch Products from a product category and all its sub-categories

Am building an E-commerce site but have run into some kind of issues

Problem

I have a category called drinks now under this category i have two sub categories which are whiskey and non-alcoholic. Under non Alcoholic we have juice and wine drinks in the parent category for whiskey and non-alcoholic while non-alcoholic is the parent category for juice and wine. the problem is when a user clicks on drinks i want them to be able to see all the items that are both in whiskey category and the children of non-alcholic category but my query to fetch the products in this categories and bring them together is not working.instead it is getting only the products of the parent category drinks which as no products

Code

public function select_from_product_page_all($cat,$level){

$que = $this->query("SELECT a.product_category_id,a.quantity, a.product_id, a.description, a.product_name, a.image, a.price, Deriv1.Count FROM `product` a  LEFT OUTER JOIN (SELECT parent, COUNT(*) AS Count FROM `product_category` GROUP BY parent) Deriv1 ON a.product_category_id = Deriv1.parent WHERE a.product_category_id=" . $cat);
return $que;
}

Database Structure

Products Table Columns are

product_name,quantity,price,image,description,product_category_id

Product Category Table Columns are

product_category_id,name,parent

what ties the product to a category is product_category_id, what ties a product_category to a parent category is the parent column and what is saved in the parent column is the product_category_id of the parent.

I was able to solve it with this query below

 public function select_from_product_page_all($cat,$level){
$que = $this->query("SELECT  p.product_category_id,p.quantity, p.product_id, p.description, p.product_name, p.image, p.price FROM product p
WHERE p.product_category_id IN (
 SELECT c.product_category_id
 FROM product_category c
WHERE c.product_category_id = " . $cat . "
UNION ALL
SELECT c2.product_category_id
FROM product_category c
  LEFT JOIN product_category c2 ON c.product_category_id = c2.parent
WHERE c.product_category_id = " . $cat . "
UNION ALL
SELECT c3.product_category_id
FROM product_category c
  LEFT JOIN product_category c2 ON c.product_category_id = c2.parent
  LEFT JOIN product_category c3 ON c2.product_category_id = c3.parent
WHERE c.product_category_id = " . $cat . "
UNION ALL
SELECT c4.product_category_id
FROM product_category c
  LEFT JOIN product_category c2 ON c.product_category_id = c2.parent
  LEFT JOIN product_category c3 ON c2.product_category_id = c3.parent
  LEFT JOIN product_category c4 ON c3.product_category_id = c4.parent
WHERE c.product_category_id = " . $cat . "
)
GROUP BY p.product_id");
return $que;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM