php - 使用parent id搜索时如何查询产品?

标签 php mysql

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 数据

最佳答案

无法调试它,并且不知道您的 lftrgth 的用途。

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

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/

相关文章:

php - MYSQL 查询计算每小时耗时

javascript - ajax, jquery with mysql query in js - 这可能吗?

php - 这是从服务层组织类的好方法吗?

java - 从 MySQL 获取数据到 Android 应用程序

javascript - php中仅通过AJAX向数据库插入数据。

php - require_once(PHPExcel/类/PHPExcel.php): failed to open stream

php - 图片库包含数据库中的缩略图,每个缩略图都与其他图像相关

php - 有没有办法像 PHP 一样自动将表单的输出转换为数组,但使用 jQuery/Javascript?

php - 如何在MySQL查询中使用数值数组常量数组?

mysql - SQL 查询获取一个字段对于另一个特定字段重复的次数