从sql查询中查找总线路由号

时间:2015-12-11 10:34:03

标签: sql-server

我正在设计公交路线数据库。我有三张桌子,如下,

LocationDetails

  Location ID Location Name
 ---------------------------
  1           A
  2           B
  3           C
  4           D
  5           E
  6           F
  7           G
  8           H
  9           I

RouteDetails

 RouteId RouteNumber
--------------------
 1       101
 2       102
 3       103

RouteLocationDetails

 RouteId LocationId
 ------------------
  1       3
  1       4
  1       5
  1       6
  2       2
  2       3
  2       4
  2       5
  2       6

我想找到用户输入的位置名称的路线编号。 例如,如果用户想要将位置从“C”移动到“F”,他们需要知道该位置可用的路线编号和路线编号,在我的情况下,2路巴士路线编号可用于该路线(Routenumber 101, 102

谁能告诉我如何为此编写sql查询?

3 个答案:

答案 0 :(得分:1)

如果您只需要处理A到B之类的简单查询,您可以自己加入RouteLocationDetails表

Intent intent = new Intent(MainActivity.this, Droidian.class);
startActivity(intent);

只是一个简单的说明,您需要加入另外两个表以获取必要的信息。

答案 1 :(得分:1)

您可以使用以下内容并确保传递两个位置名称...

    select rd.RouteNumber,r.RouteId from #LocationDetails l
    Join 
    #RouteLocationDetails r
    on 
    l.[Location ID]=r.LocationId
    join 
    #RouteDetails rd
    on 
    r.RouteId=rd.RouteId
    where [Location Name]='C'
    or [Location Name]='F'
    group by rd.RouteNumber,r.RouteId
    having count(r.LocationId)=2

<强>输出

RouteNumber RouteId
   101         1
   102         2

答案 2 :(得分:1)

我创建了以下SQL Fiddle,它适用于您的情况:https://metacpan.org/pod/Mango

创建表格并插入数据:

create table LocationDetails ([Location ID] int, [Location Name] varchar(1))
INSERT INTO LocationDetails ([Location ID],[Location Name]) VALUES(1, 'A')
INSERT INTO LocationDetails ([Location ID],[Location Name]) VALUES(2, 'B')
INSERT INTO LocationDetails ([Location ID],[Location Name]) VALUES(3, 'C')
INSERT INTO LocationDetails ([Location ID],[Location Name]) VALUES(4, 'D')
INSERT INTO LocationDetails ([Location ID],[Location Name]) VALUES(5, 'E')
INSERT INTO LocationDetails ([Location ID],[Location Name]) VALUES(6, 'F')
INSERT INTO LocationDetails ([Location ID],[Location Name]) VALUES(7, 'G')
INSERT INTO LocationDetails ([Location ID],[Location Name]) VALUES(8, 'H')
INSERT INTO LocationDetails ([Location ID],[Location Name]) VALUES(9, 'I')

create table RouteDetails (RouteId int, RouteNumber int)
INSERT INTO RouteDetails (RouteId, RouteNumber) VALUES(1, 101)
INSERT INTO RouteDetails (RouteId, RouteNumber) VALUES(2, 102)
INSERT INTO RouteDetails (RouteId, RouteNumber) VALUES(3, 103)

create table RouteLocationDetails (RouteId int, LocationId int)
INSERT INTO RouteLocationDetails (RouteId, LocationId) VALUES(1, 3)
INSERT INTO RouteLocationDetails (RouteId, LocationId) VALUES(1, 4)
INSERT INTO RouteLocationDetails (RouteId, LocationId) VALUES(1, 5)
INSERT INTO RouteLocationDetails (RouteId, LocationId) VALUES(1, 6)
INSERT INTO RouteLocationDetails (RouteId, LocationId) VALUES(2, 2)
INSERT INTO RouteLocationDetails (RouteId, LocationId) VALUES(2, 3)
INSERT INTO RouteLocationDetails (RouteId, LocationId) VALUES(2, 4)
INSERT INTO RouteLocationDetails (RouteId, LocationId) VALUES(2, 5)
INSERT INTO RouteLocationDetails (RouteId, LocationId) VALUES(2, 6)

查询from和to位置:

select RouteNumber from LocationDetails ldfrom
inner join RouteLocationDetails rldfrom on rldfrom.LocationId = ldfrom.[Location Id]
inner join RouteLocationDetails rldto on rldto.RouteId = rldfrom.RouteId
inner join LocationDetails ldto on ldto.[Location Id] = rldto.LocationId
inner join RouteDetails rd on rd.RouteId = rldto.RouteId
where ldfrom.[Location Name] = 'C' and ldto.[Location Name] = 'F'