简体   繁体   English

从产品类别及其所有子类别中获取产品

[英]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 产品名称,数量,价格,图像,描述,产品类别ID

Product Category Table Columns are 产品类别表列为

product_category_id,name,parent product_category_id,名称,父母

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. 将产品与类别关联的是product_category_id,将product_category与父类别关联的是父列,在父列中保存的是父项的product_category_id。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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