mysql - mysql 的线程类别层次结构帮助

标签 mysql database-design

enter image description here

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/

相关文章:

algorithm - 关系数据库中的时间表建模

MySQL - 如何加速 SELECT SUM

mysql - MySQL如何合并连接表的行数,包括0?

mysql - 在 SQL 数据库中存储名册的最佳方法

python - 将异构对象存储在数据库中,并使用 Python 取出它们

entity-framework - Entity Framework 端点多重性

mysql - 我想找到上升一、下降一的情况。使用SQL

.net - MySQLConnection.Open() 时出现 "Arithmetic operation resulted in an overflow"错误

MySQL 搜索精确匹配

java - MySQL 重复事件