gpt4 book ai didi

sql - 一个字段中的值作为从同一表中查找

转载 作者:行者123 更新时间:2023-12-04 13:39:27 26 4
gpt4 key购买 nike

我确定这很容易,但我在数据库方面很差...

我在 access 2003 中有下表:

title        |     id
/root | 1
/root/x | 2
/root/x/y | 3
/root/x/y/z | 4
/root/x/a | 5
/root/x/a/b | 6

即一堆节点和 ID 号 - 您可以看到/root/x 是/root/x/y 的父级。我想创建另一个表,其中包含所有节点的列表,以及它们父节点的 ID。 IE:
id  | parent id
1 | -
2 | 1
3 | 2
4 | 3
5 | 2
6 | 5

以下将为我提供父级的 id 和值:
select id, left(c.title, instrrev(c.title, "/")-1)  as parentValue from nodeIDs

产量
id |  parentNode
1 |
2 | /root
3 | /root/x
4 | /root/x/y
5 | /root/x
6 | /root/x/a

返回这些父节点的 id 而不是它们的值所需的额外步骤是什么,即在最后一个表中返回“1”而不是“/root”?

非常感谢

最佳答案

可能是这样的:

select c.id, 
left(c.title, instrrev(c.title, "/")-1) as parentValue
, p.id as parentID
from nodeIDs c
left join
nodeIDs p
on left(c.title, instrrev(c.title, "/")-1) = p.title

关于sql - 一个字段中的值作为从同一表中查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12838442/

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