简体   繁体   English

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

[英]Value in one field as lookup from same table

I'm certain this is very easy, but I am very poor at database stuff... 我敢肯定这很容易,但是我在数据库方面很差...

I have the following table in access 2003: 我在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

ie a bunch of nodes and id numbers - you can see that /root/x is the parent of /root/x/y. 即一堆节点和ID号-您可以看到/ root / x是/ root / x / y的父级。 I'd like to create another table which has a list of all the nodes, along with the id's of their parents. 我想创建另一个表,其中包含所有节点的列表以及其父节点的ID。 ie: 即:

id  | parent id
1   |   -
2   |   1
3   |   2
4   |   3
5   |   2
6   |   5

The follwing will give me the id and the value of the parent: 跟随者会给我父母的ID和值:

select id, left(c.title, instrrev(c.title, "/")-1)  as parentValue from nodeIDs

yields 产量

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

What is the extra step needed to return the id's of those parent nodes, rather than their values, ie, return '1' instead of '/root' in that last table? 返回那些父节点的ID而不是它们的值,即返回最后一个表中的“ 1”而不是“ / root”,还需要执行什么额外的步骤?

Many thanks 非常感谢

Something like this perhaps: 大概是这样的:

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

Something along these lines, I think. 我认为,遵循这些思路。

select t1.id, 
       left(t1.title, instrrev(t1.title, "/")-1)  as parentNode,
       t2.id as parentID
from nodeIDs t1
inner join nodeIDs t2 on (left(t1.title, instrrev(t1.title, "/")-1)) = t2.title

I don't have any easy way to test this. 我没有任何简单的方法可以对此进行测试。 But the basic idea is that, having derived the title of the parent node, you can do an inner join on it to get the associated id number. 但是基本思想是,在派生父节点的标题之后,您可以对其进行内部联接以获取关联的ID号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM