当前位置: 代码迷 >> Sql Server >> 对链表式的表结构查询,该怎么解决
  详细解决方案

对链表式的表结构查询,该怎么解决

热度:91   发布时间:2016-04-24 10:33:12.0
对链表式的表结构查询

求大侠赐教!!如上三张表,如何直接查出一条线路按顺序排出的所有站点。
------解决方案--------------------

create table xl(id int,name varchar(10),[status] varchar(10),update_time datetime)
insert xl 
select 1,'线路1','运营中','2014-06-18'

create table zd(id int,name varchar(10),[status] varchar(10),update_time datetime)
insert zd 
select 1,'站点1','运营中','2014-06-18' union All
select 2,'站点2','停运中','2014-06-18' union All
select 3,'站点3','运营中','2014-06-18' union All
select 4,'站点4','运营中','2014-06-18' union All
select 5,'站点5','运营中','2014-06-18' 

create table xlzd(id int,xlid int,zdid int,shangid int,xiaid int,update_time datetime)
insert xlzd
select 1,1,1,0,2,'2014-06-18' union all
select 2,1,5,4,0,'2014-06-18' union all
select 3,1,3,2,4,'2014-06-18' union all
select 4,1,2,1,3,'2014-06-18'


select a.id,b.name,c.name,d.name,e.name 
from xlzd a
inner join xl b
on a.xlid=b.id
inner join zd c
on a.zdid=c.id 
left join zd d
on a.shangid =d.id
left join zd e 
on a.xiaid =e.id
where b.status='运营中' 
and c.status='运营中' 
order by d.name

/*
1 线路1 站点1 NULL 站点2
3 线路1 站点3 站点2 站点4
2 线路1 站点5 站点4 NULL
*/

  相关解决方案