gpt4 book ai didi

mysql - SQL在同一个表中递归查找父级

转载 作者:可可西里 更新时间:2023-11-01 08:53:17 25 4
gpt4 key购买 nike

如何找到我所有项目的 parentId (mySQL)? (可能有无限数量的层次结构......不是最好的设计,但它是我正在使用的)

ID      PARENTID    NAME
9146 NULL thing1
9147 NULL thing2
9148 9146 thing3
9149 9148 thing4
9150 NULL thing5
9151 9149 thing6
9152 9147 thing7

输出应该是:

ID     REAL_PARENTID
9146 NULL (or self 9146)
9147 NULL (or self 9147)
9148 9146
9149 9146
9150 NULL (or self 9150)
9151 9146
9152 9147

最佳答案

我实际上使用 MySQL 存储过程编程编写了树遍历脚本。

我在DBA StackExchange back in Oct 24, 2011中回答过这样的问题.它还包括样本数据。

玩得开心。

关于mysql - SQL在同一个表中递归查找父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9315737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com