mysql中的表:
id | name | prent
----------------------------
1 | cat1 | 0
2 | cat1.1 | 1
3 | cat2.1 | 1
4 | cat11.2 | 2
5 | cat11.4 | 4
6 | cat1 | 0
我想在选择id 1(cat1)时,我想获得所有子类别ID。结果:" 2,3,4,5"
答案 0 :(得分:0)
如果需要选择所有子类别添加列路径
id | name | parent | path
-----------------------------------
1 | cat1 | 0 | 0-1
2 | cat1.1 | 1 | 0-1-2
3 | cat2.1 | 1 | 0-1-3
4 | cat11.2 | 2 | 0-1-2-4
5 | cat11.4 | 4 | 0-1-2-4-5
6 | cat1 | 0 | 0-6
和查询:
SELECT * FROM tree WHERE path LIKE '0-1-%' ORDER BY path ASC;
path_parent = concat('0','-',id_parent)
path_child = concat('path_parent', '-', 'id_child')