查询层次结构

时间:2018-06-07 11:14:54

标签: sql hierarchical-data recursive-query

我有层次结构问题。我有一个四级层次结构,我需要从低级别的用户看到所有的上层。我可以从低级到高级层次结构,如你所见:

select * from test1 


    **username**        **NAME**            **VALUE**
 1  e.gkioka            TEAM LEADER     x.xrysanthakopo
 2  e.gkioka            COACH           e.gkioka
 3  e.xathigianni       AGENT           e.xathigianni
 4  e.xathigianni       COACH           e.gkioka
 5  t.argentis          SUPERVISOR      t.argentis
 6  x.xrysanthakopo     SUPERVISOR      t.argentis
 6  x.xrysanthakopo     TEAM LEADER     x.xrysanthakopo

这里是用户名和角色(AGENT是低,并且是COACH,TEAM_LEADER,SUPERVISOR(上一级))

这是将层次结构从低层提升到高层的查询

 select t1.username as AGENT,t1.value as COACH ,t2.VALUE as TEAM_LEADER,t3.VALUE as SUPERVISOR
 from test1 t1,test1 t2, test1 t3
 where t1.username='e.xathigianni'
 and (t2.USERNAME=t1.VALUE and t1.name='COACH' and t2.name='TEAM LEADER') 
 and t3.USERNAME=t2.VALUE and t2.name='TEAM LEADER' and t3.name='SUPERVISOR'

给了我正确的层次结构

        AGENT           COACH           TEAM_LEADER         SUPERVISOR
 - 1  e.xathigianni     e.gkioka        x.xrysanthakopo     t.argentis

我的问题是当我把第二级(例如,(CAACH))或第三级(x.xrysanthakopo TEAM_LEADER) 它会给我正确的数据。

0 个答案:

没有答案