我详细说明一下数据库中的表1. trainInfo(基本信息表) 列车ID 车次 日期 始发站 终点站
0100000D2811 D28 20090722 哈尔滨 青岛
2.trainDetails(详细信息表)
列车ID 序列 站名 车次 到时 发时
0100000D2811 1 哈尔滨 D28 09:02 09:02
0100000D2811 2 长春 D28 10:48 10:50
0100000D2811 3 北京 D28 11:43 11:44
0100000D2811 4 济南 D28 13:05 13:07
0100000D2811 5 青岛 D28 14:25 14:26
我使用站站查询:从“哈尔滨”到“济南”select distinct a.车次,‘哈尔滨',b.发时,'济南',b.到时 from trainInfo a inner join trainDetails b on a.车次=b.车次 where a.列车ID in(
select distinct a.列车ID from
(select * from trainDetails where 站名= '哈尔滨') a,
(select * from trainDetails where 站名= '济南') b
where a.列车ID=b.列车ID and a.序列<b.序列 ) and a.train_date='20090722'
查询结果为: 车次 发站 发站的发时 到站 到站的到时
0100000D2811 哈尔滨 09:02 济南 09:02
10:48 10:50
。 。
查询结果有问题:发时和到时显示为0100000D2811车次的经过所有站点的对应时间,很多条数据
我只想显示查询站点“哈尔滨”的发时和“济南”的到时,应该只有一条数据,如: 车次 发站 发站的发时 到站 到站的到时
0100000D2811 哈尔滨 09:02 济南 13:05
如何解决?
0100000D2811 D28 20090722 哈尔滨 青岛
2.trainDetails(详细信息表)
列车ID 序列 站名 车次 到时 发时
0100000D2811 1 哈尔滨 D28 09:02 09:02
0100000D2811 2 长春 D28 10:48 10:50
0100000D2811 3 北京 D28 11:43 11:44
0100000D2811 4 济南 D28 13:05 13:07
0100000D2811 5 青岛 D28 14:25 14:26
我使用站站查询:从“哈尔滨”到“济南”select distinct a.车次,‘哈尔滨',b.发时,'济南',b.到时 from trainInfo a inner join trainDetails b on a.车次=b.车次 where a.列车ID in(
select distinct a.列车ID from
(select * from trainDetails where 站名= '哈尔滨') a,
(select * from trainDetails where 站名= '济南') b
where a.列车ID=b.列车ID and a.序列<b.序列 ) and a.train_date='20090722'
查询结果为: 车次 发站 发站的发时 到站 到站的到时
0100000D2811 哈尔滨 09:02 济南 09:02
10:48 10:50
。 。
查询结果有问题:发时和到时显示为0100000D2811车次的经过所有站点的对应时间,很多条数据
我只想显示查询站点“哈尔滨”的发时和“济南”的到时,应该只有一条数据,如: 车次 发站 发站的发时 到站 到站的到时
0100000D2811 哈尔滨 09:02 济南 13:05
如何解决?
--try:
--分组。
select a.车次,'哈尔滨',发时=min(b.发时),'济南',到时=max(b.到时)
from trainInfo a
inner join trainDetails b on a.车次=b.车次
where a.列车ID in(
select distinct a.列车ID from
(select * from trainDetails where 站名= '哈尔滨') a,
(select * from trainDetails where 站名= '济南') b
where a.列车ID=b.列车ID and a.序列 <b.序列 ) and a.train_date='20090722'
group by a.车次
到时是trainDetails表中济南的到时?
发时是trainDetails表中哈尔滨的发时
到时是trainDetails表中济南的到时
--try:
select a.车次,'哈尔滨',发时=(select 发时 from trainDetails where 列车ID=a.列车ID and 站名='哈尔滨'),
'济南',到时=(select 到时 from trainDetails where 列车ID=a.列车ID and 站名='济南')
from trainInfo a
inner join trainDetails b on a.车次=b.车次
where a.列车ID in(
select distinct a.列车ID from
(select * from trainDetails where 站名= '哈尔滨') a,
(select * from trainDetails where 站名= '济南') b
where a.列车ID=b.列车ID and a.序列 <b.序列 ) and a.train_date='20090722'
发时=(select 发时 from trainDetails where 列车ID=a.列车ID and 站名='哈尔滨'),
到时=(select 到时 from trainDetails where 列车ID=a.列车ID and 站名='济南') 有语法错误
select a.车次,a.站名, a.发时,b.站名,b.,到时 from
trainDetails a join trainDetails b
on a.站名='哈尔滨' and b.站名='济南'
and a.车次=b.车次 and a.序列<b.序列
select a.列车ID,a.站名 as 出发站,a.发时 as 发站发时,b.站名 as 到达站,b.到时 as 到站到时 from
(select * from trainDetails where 站名= '哈尔滨') a,
(select * from trainDetails where 站名= '济南') b
where a.列车ID=b.列车ID and a.序列 <b.序列 )
(select * from trainDetails where 站名= '哈尔滨') a,
(select * from trainDetails where 站名= '济南') b
where a.列车ID=b.列车ID and a.序列 <b.序列
select a.车次,a.站名, a.发时,b.站名,b.,到时 from
trainDetails a join trainDetails b
on a.站名='哈尔滨' and b.站名='济南'
and a.车次=b.车次 and a.序列<b.序列
where 列车ID in (select 列车ID from trainInfo where 日期='20090722')
create table trainInfo
(列车ID varchar(20),车次 char(6),日期 datetime,始发站 varchar(10),终点站 varchar(10))
insert into trainInfo values('0100000D2811','D28','2009-07-22','哈尔滨','青岛')
if(OBJECT_ID('trainDetails') is null)
create table trainDetails
(列车ID varchar(20),序列 int,站名 varchar(10),车次 char(6),
到时 datetime,发时 datetime)
insert into trainDetails
select '0100000D2811',1,'哈尔滨','D28','09:02','09:02'
union all select '0100000D2811',2,'哈尔滨','D28','10:48','10:50'
union all select '0100000D2811',3,'长春','D28','11:43','11:44'
union all select '0100000D2811',4,'北京','D28','13:05','13:07'
union all select '0100000D2811',5,'济南','D28','14:25','14:26'insert into traindetails
values('0100000D2821',3,'哈尔滨','D29','13:05','13:07')insert into traindetails
values('0100000D2821',6,'济南','D29','16:05','16:07')
select a.车次,a.站名, a.发时,b.站名,b.到时 from
trainDetails a join trainDetails b
on a.站名='哈尔滨' and b.站名='济南'
and a.车次=b.车次 and a.序列<b.序列 and a.列车ID=b.列车ID
where 列车ID in (select 列车ID from trainInfo where 日期='2009-07-22')
车次
D28