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

标签 sql ms-access lookup

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

我在 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/

相关文章:

python - 当查找值不存在或为 null 时 pandas 查找的默认值

mysql - 用于插入在 1 中大于最大值的值的 SQL 命令

php - 如何选择这个表?

mysql - 获取在同一订单中购买了三件指定商品的所有客户

ms-access - 限制链接表结果

ip - 在 Patricia Trie 中查找最长前缀搜索的算法/步骤

java - 如何找到继承给定接口(interface)的类实例?

PHP 在 30 分钟后插入到 MySQL

database - 当其他人正在使用 Access 后端文件时,我可以编辑它吗?

sql-server - SQL Server 与 Access 插入性能对比,尤其是在使用 GUID 时