GTFS MySQL 未找到查询列

GTFS MySQL Query Column Not Found

我正在使用导入到 MySql 的 GTFS 数据集,我正在使用 PHP 访问它。 我正在尝试查找给定时间和日期的特定路线的行程。 我的 Sql 查询如下所示

'SELECT  t.route_id AS route_id, t.service_id AS service_id, t.trip_headsign AS trip_headsign,
                t.trip_id AS trip_id,
              ( SELECT   departure_time
                FROM     stop_times
                WHERE    trip_id = t.trip_id
                ORDER BY departure_time ASC limit 1) AS initial_departure_time,
              ( SELECT   arrival_time
                FROM     stop_times
                WHERE    trip_id = t.trip_id
                ORDER BY arrival_time DESC limit 1) AS final_arrival_time
        FROM trips AS t INNER JOIN calendar_dates AS c
        WHERE t.service_id = c.service_id
          AND c.date ='.$calendar_date.' 
          AND initial_departure_time <='.$time.'
          AND'.$time. '<=final_arrival_time
          AND t.route_id ='.$route.'
        ORDER BY trip_id ASC';

$route$calendar_date$time都传入了

查询返回未找到列:

1054 Unknown column 'initial_departure_time' in 'where clause'. Im thinking that initial_departure_time cant be evaluated.

我也完全不知道如何解决这个问题。任何线索,提前致谢

您不能在 WHERE 子句中使用别名。

要么将子查询合并到 WHERE 子句中:

'SELECT t.route_id AS route_id, 
        t.service_id AS service_id, 
        t.trip_headsign AS trip_headsign,
        t.trip_id AS trip_id,
        (SELECT departure_time
         FROM stop_times
         WHERE trip_id = t.trip_id
         ORDER BY departure_time ASC limit 1) AS initial_departure_time,
        (SELECT arrival_time
         FROM stop_times
         WHERE trip_id = t.trip_id
         ORDER BY arrival_time DESC limit 1) AS final_arrival_time
FROM trips AS t INNER JOIN calendar_dates AS c
WHERE t.service_id = c.service_id
AND c.date ='.$calendar_date.' 
AND (SELECT departure_time
     FROM stop_times
     WHERE trip_id = t.trip_id
     ORDER BY departure_time ASC limit 1) <='.$time.'
AND'.$time. '<= (SELECT arrival_time
                 FROM stop_times
                 WHERE trip_id = t.trip_id
                 ORDER BY arrival_time DESC limit 1)
AND t.route_id ='.$route.'
ORDER BY trip_id ASC';

或者包装查询,然后在其中使用别名:

'SELECT * FROM (
        SELECT t.route_id AS route_id, 
        t.service_id AS service_id, 
        t.trip_headsign AS trip_headsign,
        t.trip_id AS trip_id,
        (SELECT departure_time
         FROM stop_times
         WHERE trip_id = t.trip_id
         ORDER BY departure_time ASC limit 1) AS initial_departure_time,
        (SELECT arrival_time
         FROM stop_times
         WHERE trip_id = t.trip_id
         ORDER BY arrival_time DESC limit 1) AS final_arrival_time
         FROM trips AS t INNER JOIN calendar_dates AS c
         WHERE t.service_id = c.service_id
         AND c.date ='.$calendar_date.' 
         AND t.route_id ='.$route.'
         ORDER BY trip_id ASC) a 
WHERE a.initial_departure_time <='.$time.'
AND'.$time. '<= a.final_arrival_time';

这可能会加快速度(您可能需要交换 MINMAX):

'SELECT t.route_id AS route_id, 
        t.service_id AS service_id, 
        t.trip_headsign AS trip_headsign,
        t.trip_id AS trip_id,
        MIN(s.departure_time) AS initial_departure_time,
        MAX(s.arrival_time) AS final_arrival_time
FROM trips AS t 
INNER JOIN calendar_dates AS c ON t.service_id = c.service_id
LEFT JOIN stop_times s ON s.trip_id = t.trip_id
WHERE c.date ='.$calendar_date.' 
AND t.route_id ='.$route.'
GROUP BY t.route_id, t.service_id, t.trip_headsign, t.trip_id,
HAVING '.$time.' BETWEEN MAX(s.arrival_time) AND MIN(s.departure_time)
ORDER BY trip_id ASC';