SQL server 2000 多表连接查询

来源:百度知道 编辑:UC知道 时间:2024/05/22 08:24:24
我是做一个公交查询的。
表是这样的Platform 站点表
( 字段 PlatformID(主键) PlatformName 站点名)
BusLine 线路表
( 字段 BusLineID(主键) BusLineName 线路名)
PlatformAndBusLine 站点线路表
(字段 BusLineID(外键(线路表关联)) PlatformID (外键(站点表关联)))

根据站点查询线路,把能到该站点的车列出来也就是线路,该线路的所有经过的站点也列出来。
根据起点到终点查询能到车。如果没有则转车。
我实在是想不出怎么写,求高手们帮帮忙。
首先谢谢你。能是能查到了。可是这样得到的是 编号。如过要得到Platform 表 的(PlatformName 站点名) 跟 BusLine 线路表BusLineName 线路名呢?又该怎么写 转车又该怎么写

根据站点查询线路,把能到该站点的车列出来也就是线路,该线路的所有经过的站点也列出来:
select BusLineID,PlatformID
from PlatformAndBusLine
where BusLineID in
(select BusLineID
from PlatformAndBusLine
where PlatformID in
(select PlatformID
from Platform
where PlatformName=站名))
order by BusLineID;
根据起点到终点查询能到车。
select BusLineID
from PlatformAndBusLine
where PlatformID=起点 and BusLineID in
(select BusLineID
from PlatformAndBusLine
where PlatformID=终点);
如果起点、终点是站名而非ID,则按前一方法加一层嵌套即可。