4

これは非常に簡単だと確信していますが、私はデータベース関連が非常に苦手です...

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 とともに、すべてのノードのリストを持つ別のテーブルを作成したいと思います。すなわち:

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 を返すために必要な追加の手順は何ですか?つまり、最後のテーブルで「/root」の代わりに「1」を返しますか?

どうもありがとう

4

2 に答える 2

2

おそらくこのようなもの:

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
于 2012-10-11T11:34:58.223 に答える
0

これらの線に沿った何かだと思います。

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

これをテストする簡単な方法はありません。ただし、基本的な考え方は、親ノードのタイトルを取得したら、それに内部結合を実行して関連する ID 番号を取得できるということです。

于 2012-10-11T11:35:16.577 に答える