GID name parent_GID status
1 HQ 0 # 0 means its a category
2 Blog 1 x # x some administration stuff
3 Feedback & Support 1 x
5 Service 0
6 Web 5 x
7 Advertising 6 x
8 Others 5 x
类似于(3级)
HQ
Blog
Feedback & Support
Service
Web
Advertising
Others
到目前为止我做了什么
SELECT * FROM groups WHERE status = 0 # get categories
打印
HQ
Services
所以我们可以做类似的事情
# fetch categories
# get the GIDs
# fetch with foreach GID
HQ # with GID 1 so search if there is child 1
# SELECT * FROM groups WHERE parent_GID = 1
Blog
Feedback & Support
Service # with GID 5 so search if there is child 5
# SELECT * FROM groups WHERE parent_GID = 5
Web
Others
上面的问题是它的 2 级层次结构。
有什么建议进行 3 级获取全部吗?
谢谢!
最佳答案
我之前的回答可能是一个很好的起点:
Generating Depth based tree from Hierarchical Data in MySQL (no CTEs)
提示:
更改以下语句
insert into hier select parent_cat_id, cat_id, v_depth from categories where cat_id = p_cat_id;
至
insert into hier select parent_cat_id, cat_id, v_depth from categories where parent_cat_id is null;
并且忘记传递起始parent_cat_id!
希望这有帮助:)
关于mysql - mysql 的线程类别层次结构帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7297420/