加入SQLite以获得所需的记录

问题描述 投票:1回答:1

我正在开发一个移动应用程序,它需要两个输入 - 源站名称和目标站名称。收到这两个输入后,应用程序将获取给定站点可用的列车名称及其source_arrival和destination_reach时序。

(注意:目前,我只专注于在印度西孟加拉邦运营的无保留的本地列车)

我使用SQLite作为RDBMS。我有以下三个表作为数据来源 -

  • train_table(有火车的详细信息):enter image description here
  • station_table(包含站的详细信息): enter image description here
  • route_table(包含路由详细信息): enter image description here

现在,我的目标是按照前面指定的方式生成输出(假设我将Baruipur Jn作为源并将Sealdah作为目标):

enter image description here

我无法弄清楚这需要的查询。最初,我尝试的是以下内容:

    select r1.trainId, r1.arrival as SrcArrive, r2.arrival 
    as Reach  from route_table r1 cross join route_table r2 
    where r1.trainId = r2.trainId and r1.stationId <> r2.stationId and 
    r1.arrival <> r2.arrival;

(是的,没有trainName)

但我无法减少意外的source_arrival时间。但是,我能够检索给定两个站点的不同列车的数量,具体如下:

    select _id, trainNO, trainName from train_table where _id in 
    (select trainId from route_table where stationId = 109 
    INTERSECT 
    select trainId from route_table where stationId = 21);

但有了这个,我无法得到我需要的最终结果。

sql join sqlite3 relational-database
1个回答
1
投票

这可能有效,尝试一次。

select routeData.*, train_table.* from (select r1.trainId, r1.arrival as SrcArrive, r2.arrival 
as Reach  from route_table r1 cross join route_table r2 
where r1.trainId = r2.trainId and r1.stationId <> r2.stationId and 
r1.arrival <> r2.arrival) routeData inner join train_table on routeData.trainId=train_table._id;

我已经从路由表重新定义了选择,尝试更新这个:

select trainName, SrcArrival, Destination from (select trainData.trainName, route.* from 
(select A.trainId, A.arrival as SrcArrival, B.trainId, B.arrival as Destination from 
route_table A inner join route_table B on A.trainId=B.trainId where A.stationId=109 and 
B.stationId=259 and A.arrival<B.arrival) route inner join train_table trainData on 
route.trainId=trainData._id) order by SrcArrival, Destination;
© www.soinside.com 2019 - 2024. All rights reserved.