如何在使用父ID进行搜索时查询产品?

时间:2015-02-01 15:34:50

标签: php mysql

  

参考

     

How to count product in this category?

     

TABLE CATEGORY

 category_id  
 title 
 lft 
 rght 
 parent 
 level
  

表格产品

 product_id 
 category_id 
 title
  

数据表类别

|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

但不符合要求。

如果我要搜索电视类别_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表类别中的数据

1 个答案:

答案 0 :(得分:0)

无法对其进行调试,也不知道您的lftrgth是什么。

但要获得所需的结果,您可以尝试:

SELECT product.*
from product
LEFT JOIN category as cat 
ON product.category_id = cat.category_id 
WHERE cat.parent = 2