我正在尝试连接两个表,其中表类别有一个列category.parent_id(这是允许区分类别和子类别的内容)
SELECT c.category_ID
, c.category_name
, c.is_active
, p.name
, p.cost
, p.image
, p.image_link
FROM category c
JOIN products p
ON p.category_ID = c.category_ID
AND c.category_ID = '$sql_category_id'
$ sql_category_id = 29;只有更多(子)类别,查询应返回列
category`.`category_ID`, `category`.`category_name`, `category`.`is_active`
来自查询$ sql_category_id = 29的category.category_ID列的结果应为30,31,32
在$ sql_category_id = 30(或31和32)中,它应该仅返回产品元素,因为没有子类别。
问题是,它返回产品详细信息(当id = 30,31,32时),但是当它应该列出子类别详细信息时它不会(id = 29)
如我所知,我加入了一个示例表:
table product
product_id | category_id | name | cost
1 | 28 | Aloe Vera Cream | 5.97
2 | 30 | Macadamia 100ml Base Oil | 6.35
3 | 30 | Hazelnut 100ml Base Oil | 6.00
4 | 31 | Clear Skin 100ml Massage Oil | 4.5
table category
category_id | category_name | is_active | parent_id
11 | Aromatherapy | 1 | 2
28 | Lotions and Potions | 1 | 11
29 | Massage and Base Oils | 1 | 11
30 | 100ml Base Oils | 1 | 29
31 | 100ml Massage and Bath Oils | 1 | 29
如果我想列出'29'类别,它应该返回'category_name''100ml Base Oils'和'100ml Massage and Bath Oils'; 列出类别'30'它应该返回'product_id''2'(澳洲坚果100ml基础油)和'3'('榛子100ml基础油') 而且我们也有这样的情况:
table product
product_id | category_id | name | cost
5 | 29 | Example Product | 1.21
在这种情况下,它应该有3个结果:
result 1: '100ml Base Oils'
result 2: '100ml Massage and Bath Oils'
result 3: 'product_id 5' 'Example Product'
它适用于产品收听的当前查询:
http://marques.byethost3.com/product.php?cat_id=30
不适用于听力类别:
http://marques.byethost3.com/product.php?cat_id=29