小弟现在正在做一个项目《列车时刻查询系统》,有如下两张数据表create table T_Schedule
(
ScheduleCode varchar(10) not null primary key,
fromCity varchar(20) not null,
toCity varchar(20) not null,
LeaveTime datetime not null default(getdate()),
ArrivalTime datetime not null default(getdate()),
TrainType varchar(10) not null,
Speed varchar(10) not null,
Distance int not null,
Days int not null
)
create table T_Schedule_Detail
(
cid int identity(1,1) not null primary key,
ScheduleCode varchar(10) not null foreign key references T_Schedule(ScheduleCode),
cityName varchar(20) not null,
ArrivalTime datetime null default(getdate()),
LeaveTime datetime null default(getdate()),
NumOfSchedule int not null,
Distance int not null,
Days int not null
)输入的测试数据如下所示T_Schedule表的数据1051 天津 齐齐哈尔 2008-07-22 15:07:00.000 2008-07-22 11:00:00.000 普通特快 300公里 1474 1
6375 长春 吉林 2008-07-22 15:34:00.000 2008-07-22 18:40:00.000 普通特快 200公里 128 0
7552 乌鲁木齐 哈密 2008-07-22 10:31:00.000 2008-07-22 19:59:00.000 动车组 300公里 553 1
D543 北京 天津 2008-07-22 15:45:00.000 2008-07-22 16:56:00.000 普通列车 180公里 137 0
D556 天津 北京 2008-07-22 09:14:00.000 2008-07-22 10:26:00.000 普通列车 180公里 137 0
D567 北京 石家庄 2008-07-23 14:44:00.000 2008-07-23 16:46:00.000 普通特快 200公里 277 0
K370 天津 大连 2008-07-22 01:39:00.000 2008-07-22 12:23:00.000 特快列车 360公里 2244 1
K454 乌鲁木齐 成都 2008-07-22 14:05:00.000 2008-07-22 15:12:00.000 普通特快 200公里 3026 2
K544 乌鲁木齐 玉门 2008-07-22 09:53:00.000 2008-07-22 09:29:00.000 动车组 256公里 3358 2
K596 乌鲁木齐 南京 2008-07-22 11:51:00.000 2008-07-22 13:30:00.000 特快列车 360公里 3782 2
T225 北京 大连 2008-07-23 18:16:00.000 2008-07-23 05:38:00.000 特快列车 300公里 1066 1T_Schedule_Detail表的数据1 1051 天津 2008-07-23 15:07:00.000 2008-07-23 15:07:00.000 1 0 0
2 1051 唐山 2008-07-23 16:24:00.000 2008-07-23 16:30:00.000 2 123 0
3 1051 滦县 2008-07-23 17:15:00.000 2008-07-23 17:34:00.000 3 194 0
4 1051 北戴河 2008-07-23 18:34:00.000 2008-07-23 18:38:00.000 4 263 0
5 1051 秦皇岛 2008-07-23 18:58:00.000 2008-07-23 19:02:00.000 5 285 0
6 1051 山海关 2008-07-23 19:22:00.000 2008-07-23 19:30:00.000 6 301 0
7 1051 绥中 2008-07-23 20:12:00.000 2008-07-23 20:15:00.000 7 366 0
8 1051 兴城 2008-07-23 20:44:00.000 2008-07-23 20:49:00.000 8 414 0
9 1051 锦州 2008-07-23 21:43:00.000 2008-07-23 21:50:00.000 9 485 0
10 1051 沟帮子 2008-07-23 22:29:00.000 2008-07-23 22:34:00.000 10 549 0
11 1051 大虎山 2008-07-23 23:01:00.000 2008-07-23 23:06:00.000 11 591 0
12 1051 沈阳北 2008-07-23 01:05:00.000 2008-07-23 01:19:00.000 12 718 1
13 1051 四平 2008-07-23 03:12:00.000 2008-07-23 03:14:00.000 13 903 1
14 1051 郑家屯 2008-07-23 04:15:00.000 2008-07-23 04:25:00.000 14 996 1
15 1051 保康 2008-07-23 05:16:00.000 2008-07-23 05:28:00.000 15 1073 1
16 1051 太平川 2008-07-23 05:52:00.000 2008-07-23 05:57:00.000 16 1107 1
17 1051 开通 2008-07-23 06:30:00.000 2008-07-23 06:32:00.000 17 1156 1
18 1051 洮南 2008-07-23 07:15:00.000 2008-07-23 07:17:00.000 18 1224 1
19 1051 白城 2008-07-23 07:40:00.000 2008-07-23 07:48:00.000 19 1256 1
20 1051 镇赉 2008-07-23 08:15:00.000 2008-07-23 08:21:00.000 20 1294 1
21 1051 泰来 2008-07-23 09:01:00.000 2008-07-23 09:03:00.000 21 1358 1
22 1051 平洋 2008-07-23 09:23:00.000 2008-07-23 09:27:00.000 22 1384 1
24 1051 江桥 2008-07-23 09:47:00.000 2008-07-23 09:50:00.000 23 1406 1
25 1051 三间房 2008-07-23 10:31:00.000 2008-07-23 10:34:00.000 24 1444 1
28 1051 齐齐哈尔 2008-07-23 11:00:00.000 2008-07-23 11:00:00.000 25 1474 1
29 6375 长春 2008-07-23 15:34:00.000 2008-07-23 15:34:00.000 1 0 0
30 6375 龙泉 2008-07-23 15:46:00.000 2008-07-23 15:51:00.000 2 8 0
31 6375 兴隆山 2008-07-23 15:59:00.000 2008-07-23 16:01:00.000 3 16 0
32 6375 卡伦 2008-07-23 16:10:00.000 2008-07-23 16:12:00.000 4 25 0
33 6375 龙家堡 2008-07-23 16:21:00.000 2008-07-23 16:23:00.000 5 35 0
34 6375 饮马河 2008-07-23 16:32:00.000 2008-07-23 16:34:00.000 6 43 0
35 6375 九台 2008-07-23 16:43:00.000 2008-07-23 16:46:00.000 7 52 0
36 6375 营城 2008-07-23 16:54:00.000 2008-07-23 17:14:00.000 8 59 0
37 6375 土们岭 2008-07-23 17:27:00.000 2008-07-23 17:28:00.000 9 73 0
38 6375 左家 2008-07-23 17:40:00.000 2008-07-23 17:40:00.000 10 78 0
39 6375 河湾子 2008-07-23 17:44:00.000 2008-07-23 17:46:00.000 11 86 0
40 6375 桦皮厂 2008-07-23 17:54:00.000 2008-07-23 17:56:00.000 12 93 0
41 6375 孤店子 2008-07-23 18:06:00.000 2008-07-23 18:08:00.000 13 104 0
42 6375 新九站 2008-07-23 18:16:00.000 2008-07-23 18:18:00.000 14 113 0
43 6375 吉林 2008-07-23 18:40:00.000 2008-07-23 18:40:00.000 15 128 0
44 7552 乌鲁木齐 2008-07-23 10:31:00.000 2008-07-23 10:31:00.000 1 0 0
45 7552 乌拉泊 2008-07-23 10:46:00.000 2008-07-23 10:47:00.000 2 9 0
46 7552 三葛庄 2008-07-23 11:18:00.000 2008-07-23 11:19:00.000 3 33 0
47 7552 柴窝堡 2008-07-23 11:35:00.000 2008-07-23 11:36:00.000 4 51 0现在小弟想实现,当输入起始站和终点站的时候可以查询出如下结果,就是起点站到终点站的所经城市信息,如下所示(北京-天津)站次            经停战             发车时间           里程             天数62 北京 2008-07-23 15:45:00.000  0         1
63 天津 2008-07-23 16:56:00.000  137         2
小弟想了很长时间了请高手指点迷津

解决方案 »

  1.   

    是啊请给出SQL语句好么?我很困惑
      

  2.   

    判断天津跟北京的ScheduleCode 是不是一样
    一个的就取出来
    没有就不用取了
    直接对T_Schedule_Detail表查找就可以了吧
      

  3.   


    select b.cid,b.cityName,b.ArrivalTime,b.Distance,b.Days  from T_Schedule a inner join T_Schedule_Detail b on a.ScheduleCode=b.ScheduleCode
    order by b.cid
      

  4.   

    建一个视图不就什么问题都解决了,
    用ScheduleCode 进行关联
    T_Schedule_Detail作为主表
      

  5.   

    select b.cid,b.cityName,b.ArrivalTime,b.Distance,b.Days  from T_Schedule a inner join T_Schedule_Detail b on a.ScheduleCode=b.ScheduleCode 
    and a.fromCity='北京' and a.toCity='天津'
    order by b.cid
      

  6.   

    DECLARE @ST VARCHAR(20),@ED VARCHAR(20)
    SELECT @ST='北戴河',@ED='洮南'SELECT 站次=CID, 经停战=cityName,发车时间=LeaveTime,里程=Distance,天数=Days   FROM T_Schedule_Detail
    WHERE (cityName=@ST OR cityName=@ED)
      AND EXISTS
    (
    SELECT 1 FROM T_Schedule_Detail A
     INNER JOIN T_Schedule_Detail B
       ON A.cityName=@ST AND B.cityName=@ED AND A.ScheduleCode=B.ScheduleCode)
      

  7.   

    insert into T_Schedule
    select '1051', '天津', '齐齐哈尔', '2008-07-22 15:07:00.000', '2008-07-22 11:00:00.000', '普通特快', '300公里', 1474, 1 union all
    select '1052', '天津', '齐齐哈尔', '2008-07-22 15:07:00.000', '2008-07-22 11:00:00.000', '普通特快', '300公里', 1474, 1
    go
    insert into T_Schedule_Detail
    select '1051', '天津', '2008-07-23 15:07:00.000', '2008-07-23 15:07:00.000', 1, 0, 0 union all
    select '1051', '唐山', '2008-07-23 16:24:00.000', '2008-07-23 16:30:00.000', 2, 123, 0 union all
    select '1051', '滦县', '2008-07-23 17:15:00.000', '2008-07-23 17:34:00.000', 3, 194, 0 union all
    select '1052', '天津', '2008-07-23 15:07:00.000', '2008-07-23 15:07:00.000', 11, 0, 0 union all
    select '1052', 'test', '2008-07-23 16:24:00.000', '2008-07-23 16:30:00.000', 12, 123, 0 union all
    select '1052', '滦县', '2008-07-23 17:15:00.000', '2008-07-23 17:34:00.000', 13, 194, 0
    declare @StartStation nvarchar(20), @EndStation nvarchar(20)
    set @StartStation = '天津'
    set @EndStation = '滦县'select [站次] = NumOfSchedule, [经停站] = cityName, [发车时间] = LeaveTime, [里程] = Distance, [天数] = Days 
    from T_Schedule_Detail d
    where ScheduleCode in
    (
    select ScheduleCode from T_Schedule s
    where exists(select * 
                 from T_Schedule_Detail a, T_Schedule_Detail b
                 where a.ScheduleCode = s.ScheduleCode and b.ScheduleCode = s.ScheduleCode
                     and a.ScheduleCode = b.ScheduleCode and a.cityName = @StartStation and b.cityName = @EndStation)
    )
        and NumOfSchedule >= (select NumOfSchedule from T_Schedule_Detail where ScheduleCode = d.ScheduleCode and cityName = @StartStation) 
        and NumOfSchedule <= (select NumOfSchedule from T_Schedule_Detail where ScheduleCode = d.ScheduleCode and cityName = @EndStation)
      

  8.   

    DECLARE @ST VARCHAR(20),@ED VARCHAR(20)
    SELECT @ST='北戴河',@ED='洮南'SELECT 站次=CID, 经停战=cityName,发车时间=LeaveTime,里程=Distance,天数=Days   FROM T_Schedule_Detail
    WHERE (cityName=@ST OR cityName=@ED)
      AND EXISTS
    (
    SELECT 1 FROM T_Schedule_Detail A
     INNER JOIN T_Schedule_Detail B
       ON A.cityName=@ST AND B.cityName=@ED AND A.ScheduleCode=B.ScheduleCode)
      

  9.   

    if object_id('T_Schedule_Detail') is not null drop table T_Schedule_Detail
    go
    create table T_Schedule_Detail
    (
        cid int identity(1,1) not null primary key,
        ScheduleCode varchar(10) not null ,
        cityName varchar(20) not null,
        ArrivalTime datetime null default(getdate()),
        LeaveTime datetime null default(getdate()),
        NumOfSchedule int not null,
        Distance int not null,
        Days int not null
    )insert into T_Schedule_Detail
    select '1051', '天津', '2008-07-23 15:07:00.000', '2008-07-23 15:07:00.000', 1, 0, 1 union all
    select '1051', '唐山', '2008-07-23 16:24:00.000', '2008-07-23 16:30:00.000', 2, 123, 2 union all
    select '1051', '滦县', '2008-07-23 17:15:00.000', '2008-07-23 17:34:00.000', 3, 194, 3 union all
    select '1052', '天津', '2008-07-23 15:07:00.000', '2008-07-23 15:07:00.000', 11, 0, 1 union all
    select '1052', 'test', '2008-07-23 16:24:00.000', '2008-07-23 16:30:00.000', 12, 123, 2 union all
    select '1052', '滦县', '2008-07-23 17:15:00.000', '2008-07-23 17:34:00.000', 13, 194, 3DECLARE @ST VARCHAR(20),@ED VARCHAR(20)
    SELECT @ST='天津',@ED='test'SELECT 站次=CID, 经停战=cityName,发车时间=LeaveTime,里程=Distance,天数=Days 
       FROM T_Schedule_Detail
    WHERE (cityName=@ST OR cityName=@ED)
      AND ScheduleCode IN
    (
    SELECT A.ScheduleCode FROM T_Schedule_Detail A
     INNER JOIN T_Schedule_Detail B
      ON A.cityName=@ST AND B.cityName=@ED AND A.ScheduleCode=B.ScheduleCode)
    /*站次          经停战                  发车时间                                                   里程          天数          
    ----------- -------------------- ------------------------------------------------------ ----------- ----------- 
    4           天津                   2008-07-23 15:07:00.000                                0           1
    5           test                 2008-07-23 16:30:00.000                                123         2(所影响的行数为 2 行)
    */
      

  10.   


    借用楼上的测试数据if object_id('T_Schedule_Detail') is not null drop table T_Schedule_Detail
    go
    create table T_Schedule_Detail
    (
        cid int identity(1,1) not null primary key,
        ScheduleCode varchar(10) not null ,
        cityName varchar(20) not null,
        ArrivalTime datetime null default(getdate()),
        LeaveTime datetime null default(getdate()),
        NumOfSchedule int not null,
        Distance int not null,
        Days int not null
    )insert into T_Schedule_Detail
    select '1051', '天津', '2008-07-23 15:07:00.000', '2008-07-23 15:07:00.000', 1, 0, 1 union all
    select '1051', '唐山', '2008-07-23 16:24:00.000', '2008-07-23 16:30:00.000', 2, 123, 2 union all
    select '1051', '滦县', '2008-07-23 17:15:00.000', '2008-07-23 17:34:00.000', 3, 194, 3 union all
    select '1052', '滦县', '2008-07-23 15:07:00.000', '2008-07-23 15:07:00.000', 11, 0, 1 union all
    select '1052', 'test', '2008-07-23 16:24:00.000', '2008-07-23 16:30:00.000', 12, 123, 2 union all
    select '1052', '天津', '2008-07-23 17:15:00.000', '2008-07-23 17:34:00.000', 13, 194, 3 union all
    select '1052', 'test2', '2008-07-23 17:15:00.000', '2008-07-23 17:34:00.000', 13, 194, 3
    select c.ScheduleCode,c.cityName,c.Distance from T_Schedule_Detail c inner join
    (select ScheduleCode,cityName,Distance from T_Schedule_Detail where cityName='天津') a
    on c.ScheduleCode=a.ScheduleCode and c.Distance>=a.Distance inner join
    (select ScheduleCode,cityName,Distance from T_Schedule_Detail where cityName='滦县') b
    on a.ScheduleCode=b.ScheduleCode and c.Distance<=b.Distance
    (所影响的行数为 7 行)ScheduleCode cityName             Distance    
    ------------ -------------------- ----------- 
    1051         天津                   0
    1051         唐山                   123
    1051         滦县                   194(所影响的行数为 3 行)