这是我的SQL查询
SELECT
Routes.route_id, Routes.route_desc, Routes.route_type,
Trips.service_id, Trips.trip_id, Trips.route_direction,
Stop_times.stop_id, Stop_times.arrival_time, Stop_times.departure_time,
Stops.stop_name, Calendar.start_date, Calendar.end_date
FROM Trips
INNER JOIN Routes ON Trips.route_id = Routes.route_id
INNER JOIN Stop_times ON Trips.trip_id = Stop_times.trip_id
INNER JOIN Stops ON Stop_times.stop_id = Stops.stop_id
INNER JOIN Calendar ON Trips.service_id = Calendar.service_id
WHERE
Calendar.start_date = strftime('%Y%m%d','now') and
CAST(REPLACE(Stop_times.arrival_time, ':', '') as decimal)
>= strftime('%H%M%S', 'now', 'localtime')
and Trips.direction_id = 0
and Stop_times.stop_id = '279018'
ORDER BY Stop_times.arrival_time asc
1>我的结果是这样的
(还有许多其他列)
2>我想使用第一行trip_id并将其作为
SELECT
Routes.route_id, Routes.route_desc, Routes.route_type,
Trips.service_id, Trips.trip_id, Trips.route_direction,
Stop_times.stop_id, Stop_times.arrival_time, Stop_times.departure_time,
Stops.stop_name, Calendar.start_date, Calendar.end_date
FROM Trips
INNER JOIN Routes ON Trips.route_id = Routes.route_id
INNER JOIN Stop_times ON Trips.trip_id = Stop_times.trip_id
INNER JOIN Stops ON Stop_times.stop_id = Stops.stop_id
INNER JOIN Calendar ON Trips.service_id = Calendar.service_id
WHERE
Calendar.start_date = strftime('%Y%m%d','now') and
CAST(REPLACE(Stop_times.arrival_time, ':', '') as decimal)
>= strftime('%H%M%S', 'now', 'localtime')
and Trips.direction_id = 0
and Stop_times.trip_id = 'what ever on first row from above query example 551.22018xxxxx'
ORDER BY Stop_times.arrival_time asc
结果应该如下
谢谢。
答案 0 :(得分:1)
您可以使用subquery
包装结果并使用结果执行过滤器。
您的查询应如下所示:
SELECT *
FROM(
SELECT
Routes.route_id, Routes.route_desc, Routes.route_type,
Trips.service_id, Trips.trip_id, Trips.route_direction,
Stop_times.stop_id, Stop_times.arrival_time, Stop_times.departure_time,
Stops.stop_name, Calendar.start_date, Calendar.end_date
FROM Trips
INNER JOIN Routes ON Trips.route_id = Routes.route_id
INNER JOIN Stop_times ON Trips.trip_id = Stop_times.trip_id
INNER JOIN Stops ON Stop_times.stop_id = Stops.stop_id
INNER JOIN Calendar ON Trips.service_id = Calendar.service_id
WHERE
Calendar.start_date = strftime('%Y%m%d','now') and
CAST(REPLACE(Stop_times.arrival_time, ':', '') as decimal)
>= strftime('%H%M%S', 'now', 'localtime')
and Trips.direction_id ={}
and Stops_Time.Stop_ID = 'xxxx'
ORDER BY Stop_times.arrival_time ASC)
WHERE Trip_ID = @Trip_ID
编辑:
Stop_Times.Stop_ID
的第一行只需要一行,因此与您选择的第一行包装相同的条件,然后添加limit
SELECT
Routes.route_id, Routes.route_desc, Routes.route_type,
Trips.service_id, Trips.trip_id, Trips.route_direction,
Stop_times.stop_id, Stop_times.arrival_time, Stop_times.departure_time,
Stops.stop_name, Calendar.start_date, Calendar.end_date
FROM Trips
INNER JOIN Routes ON Trips.route_id = Routes.route_id
INNER JOIN Stop_times ON Trips.trip_id = Stop_times.trip_id
INNER JOIN Stops ON Stop_times.stop_id = Stops.stop_id
INNER JOIN Calendar ON Trips.service_id = Calendar.service_id
WHERE
Calendar.start_date = strftime('%Y%m%d','now') and
CAST(REPLACE(Stop_times.arrival_time, ':', '') as decimal)
>= strftime('%H%M%S', 'now', 'localtime')
and Trips.direction_id ={}
and Stop_Times.Trip_ID =
(SELECT Stop_Times.Trip_ID
FROM Trips
INNER JOIN Stop_times ON Trips.trip_id = Stop_times.trip_id
INNER JOIN Stops ON Stop_times.stop_id = Stops.stop_id
INNER JOIN Calendar ON Trips.service_id = Calendar.service_id
WHERE
Calendar.start_date = strftime('%Y%m%d','now') and
CAST(REPLACE(Stop_times.arrival_time, ':', '') as decimal)
>= strftime('%H%M%S', 'now', 'localtime')
and Trips.direction_id ={}
ORDER BY Stop_times.arrival_time ASC LIMIT 1)
ORDER BY Stop_times.arrival_time ASC