refer to
How to count product in this category?
TABLE CATEGORY
category_id
title
lft
rght
parent
level
TABLE PRODUCT
product_id
category_id
title
DATA Table category
|1|Electronics|1|16|0|0 |2|Televisions|2|7|1|1 |3|LCD|3|4|2|2 |4|PLASMA|5|6|2|2 |5|Players|8|15|1|1 |6|Mp3 players|9|10|5|2 |7|CD players|11|12|5|2 |8|DVD players|13|14|5|2 |9|Furniture|17|18|0|0
如果我想查询产品以显示我将在表产品中查询的所有内容
SELECT * from product order by product_id
但是如果我想通过父id查询我该如何查询?
我尝试查询
select product.*
from category as node, category as parent, product
where node.lft between parent.lft and parent.rght
and node.category_id = product.category_id
group by parent.title
order by node.lft
但不符合要求。
如果我要搜索电视category_id = 2 我想在category_id 2相同示例中显示产品
Example
|1|4|Plasma 1
|2|4|Plasma 2
|3|4|Plasma 3
|4|4|Plasma 4
|5|4|Plasma 5
|6|3|LCD 1
|7|3|LCD 2
|8|3|LCD 3
请帮助我。 谢谢您的解答。
http://i.stack.imgur.com/2kxzt.png表类别中的 img 数据
最佳答案
无法调试它,并且不知道您的 lft
和 rgth
的用途。
但是要获得您需要的结果,您可以尝试:
SELECT product.*
from product
LEFT JOIN category as cat
ON product.category_id = cat.category_id
WHERE cat.parent = 2
关于php - 使用parent id搜索时如何查询产品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28264420/