我有两个表(类别,产品),如下:
类别表:
cid name parent
1 items Null
2 A 1
3 aa 2
4 ab 2
5 ac 2
6 B 1
7 ba 5
8 bb 5
9 bc 5
10 C 1
11 ca 9
12 cb 9
13 cc 9
产品表:
pid cid pname
1 2 p1
2 3 p2
3 4 p3
4 4 p4
5 5 p5
6 5 p6
[这里,我想获取特定父母的所有父母和子女产品。这里有cid = 2
,然后需要cid=2
及其子级的所有乘积。
我在这里尝试过类似的方法,但是不确定如何使用此查询将product
表联接在一起:
select cid,
name,
parent
from (select * from categories
order by parent, cid) categories,
(select @pv := '2') initialisation
where find_in_set(parent, @pv) > 0
and @pv := concat(@pv, ',', cid)
有人可以帮我吗?谢谢。
从产品中选择product.pname,product.cid,ategory.parentproduct.cid = catgeory.cid上的内部联接类别;