注册 登录
编程论坛 SQL Server论坛

关于公交查询的算法~~希望大家有时间帮忙指点一二啊!!!

v123981499 发布于 2008-06-04 21:23, 1158 次点击
这是关于中转站换乘的一个存储过程,在执行查询的时候,直达路线可显示,但换乘就没有显示了(即else后的语句),貌似也没有什么语法错误。第一个if是起点和终点在一条线路上的,else是关于中转站的查找及换车的路线。其中,lineid为线路的名称,stationname为站点的名称,希望大家可以帮帮我,找出其中的错误和不足之处,小妹在此感激不尽!!! ( 可QQ联系:123981499,请注明来意,谢谢!)

CREATE procedure s_to_s_select
(  
 @BeginStation varchar(20),
 @EndStation   varchar(20)
)
as
if exists     
 (     
        select *                   --起点和终点在同一条线路上
        from 线路表 as a
        where StationName=@BeginStation   
        and      
        StationName in                        
                            (
                                select StationName           
                                 from 线路表  as b
                                  where StationName=@EndStation   
                                   and  a.LineID=b.LineID
       )
  )      
                           
     select *                   --起点和终点在同一条线路上
        from 线路表 as a
        where StationName=@BeginStation
        and      
        StationName in                        
                            (
                                select StationName           
                                 from 线路表  as b
                                  where StationName=@EndStation   
                                   and  a.LineID=b.LineID
       )   order by Turn
   
else   
      if exists               --经过两站的不同线路中有交点
     (  select *                       
        from 线路表 as a
        where stationname in (                --经过起点的线路上的所有点
                                           select stationname
                                           from 线路表 as b
                                           where lineid in (
                                                                    select lineid
                                                                     from 线路表 as c
                                                                      where stationname like rtrim (@BeginStation)
                                                                  )
                                           )
       and      
                  stationname in (                        --过终点
                                           select stationname
                                           from 线路表 as b
                                           where lineid in (
                                                                    select lineid
                                                                     from 线路表 as c
                                                                      where stationname like rtrim (@EndStation)
                                                                  )
                                           )
                  )
        select *                       --查找线路
        from 线路表 as a
        where stationname in (
                                           select stationname
                                           from 线路表  as b
                                           where lineid in (
                                                                    select lineid
                                                                     from 线路表 as c
                                                                      where stationname like rtrim (@BeginStation)
                                                                  )
                                           )
       and     
                  stationname in (
                                           select stationname
                                           from 线路表 as b
                                           where lineid in (
                                                                    select lineid
                                                                     from 线路表 as c
                                                                      where stationname  like rtrim (@EndStation)
                                                                  )  
                                           )
        and     lineid in (
                            select lineid
                            from 线路表  as b
                             where stationname like rtrim (@BeginStation)                          
                           )
        and     lineid in (
                            select lineid
                            from 线路表  as b
                             where stationname like rtrim (@EndStation)           
                           )
       order by turn
GO
4 回复
#2
v1239814992008-06-14 13:04
有没有人帮帮我啊!!!!
#3
makebest2008-06-14 20:13
换乘一次的查询语句
select b1.StationName,b1.LineID,b2.StationName,B3.LineID,b4.StationName
from 线路表 as b1,
线路表 as b2,
线路表 as b3,
线路表 as b4
where b1.StationName=@BeginStation
and b1.LineID = b2.LineID
and b2.StationName = b3.StationName and b2.LineID<>b3.LineID
and b3.LineID = b4.LineID
and b4.StationName = @EndStation
#4
球球2008-06-15 09:27
create proc proc_train
@startaddress varchar(5),
@endaddress varchar(5)
as
select s.schedulecode,s.cityname into #ttb1 from (select * from T_schedule_detail where schedulecode in
(select schedulecode from T_schedule_detail where cityname=@startaddress)) s,
(select * from T_schedule_detail where schedulecode in
(select schedulecode from T_schedule_detail  where cityname=@endaddress )) e where s.cityname=e.cityname

select A.schedulecode,A.fromcity,A.tocity,A.leavetime,A.arrivaltime,A.traintype,A.speed ,A.distance,A.days,B.cityname into #ttb2 from T_schedule A,ttb1 B where A.schedulecode=B.schedulecode

select A.schedulecode,A.fromcity,A.tocity,A.leavetime,A.arrivaltime,A.traintype,A.speed ,A.distance,A.days,B.cityname into #ttb_line_midway from ttb1 B,ttb2 A where A.schedulecode=B.schedulecode
go
#5
v1239814992008-06-15 17:44
谢谢 3楼的 ,虽然 4楼的似乎比较好,但是以本人目前的知识结构理解起来还是有点困难的,所以采用了3楼,呵呵
1