亲子关系 [英] parent child relations
本文介绍了亲子关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
选择父类别时将产品分配到子类别的最佳方法是什么?
What is the best way of getting products assigned to child categories when the parent category is selected?
即如果我选择顶级类别,我希望获得所有子类别中的所有产品,而不仅仅是分配到当前所选级别的产品.
i.e If I select the top level category I'd like to get all the products in all the sub categories not just products assigned to the current level selected.
因此选择类别 1 将返回 prod_id 的 1 和 2.选择类别 2 将返回 prod_id 2.
so selecting category 1 will return prod_id's 1 and 2. Selecting category 2 will return prod_id 2.
**category**
id parent_id name
1 0 Vegetables
2 1 Tomatoes
3 2 Yellow Tomatoes
**product_category**
prod_id cat_id
1 1
2 3
**products**
prod_id name
1 Carrot
2 Small Yellow Tomato
推荐答案
这个查询是您要查找的吗?
is this query what you're looking for?
$query =<<<MYSQL
SELECT `prod_id`
FROM `produdct_category`
INNER JOIN `category` ON `product_category`.`cat_id` = `category`.`id`
WHERE `category`.`parent_id` = {$selectedParentId}
MYSQL;
更新的问题,更新的答案:
Updated question, updated answer:
$query =<<<MYSQL
SELECT *
FROM `produdcts`
INNER JOIN `produdct_category` ON `products`.`prod_id` = `produdct_category`.`prod_id`
INNER JOIN `category` ON `product_category`.`cat_id` = `category`.`id`
WHERE `category`.`id` = {$selectedCatId}
OR `category`.`parent_id` = {$selectedCatId}
MYSQL;
这篇关于亲子关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文