代码之家  ›  专栏  ›  技术社区  ›  Alex L

MySQL查询不返回任何数据

  •  2
  • Alex L  · 技术社区  · 14 年前

    在我指定时间段之前,查询工作正常。我指定时间段的方式有问题吗?我知道在这个时间范围内有很多条目。

    此查询返回空:

    SELECT  stop_times.stop_id, STR_TO_DATE(stop_times.arrival_time, '%H:%i:%s') as stopTime, routes.route_short_name, routes.route_long_name, trips.trip_headsign  FROM trips 
    JOIN stop_times ON trips.trip_id = stop_times.trip_id 
    JOIN routes ON routes.route_id = trips.route_id
    WHERE  stop_times.stop_id = 5508
    HAVING stopTime BETWEEN DATE_SUB(stopTime,INTERVAL 1 MINUTE) AND  DATE_ADD(stopTime,INTERVAL 20 MINUTE);
    

    下面是解释:

    +----+-------------+------------+--------+------------------+---------+---------+-------------------------------+------+-------------+
    | id | select_type | table      | type   | possible_keys    | key     | key_len | ref                           | rows | Extra       |
    +----+-------------+------------+--------+------------------+---------+---------+-------------------------------+------+-------------+
    |  1 | SIMPLE      | stop_times | ref    | trip_id,stop_id  | stop_id | 5       | const                         |  605 | Using where |
    |  1 | SIMPLE      | trips      | eq_ref | PRIMARY,route_id | PRIMARY | 4       | wmata_gtfs.stop_times.trip_id |    1 |             |
    |  1 | SIMPLE      | routes     | eq_ref | PRIMARY          | PRIMARY | 4       | wmata_gtfs.trips.route_id     |    1 |             |
    +----+-------------+------------+--------+------------------+---------+---------+-------------------------------+------+-------------+
    3 rows in set (0.00 sec)
    

    +---------+----------+------------------+-----------------+---------------+
    | stop_id | stopTime | route_short_name | route_long_name | trip_headsign |
    +---------+----------+------------------+-----------------+---------------+
    |    5508 | 06:31:00 | "80"             | ""              | "FORT TOTTEN" |
    |    5508 | 06:57:00 | "80"             | ""              | "FORT TOTTEN" |
    |    5508 | 07:23:00 | "80"             | ""              | "FORT TOTTEN" |
    |    5508 | 07:49:00 | "80"             | ""              | "FORT TOTTEN" |
    |    5508 | 08:15:00 | "80"             | ""              | "FORT TOTTEN" |
    |    5508 | 08:41:00 | "80"             | ""              | "FORT TOTTEN" |
    |    5508 | 09:08:00 | "80"             | ""              | "FORT TOTTEN" |
    

    我正在使用 Google Transit format Data 加载到MySQL。

    查询应该提供给定公交车站的停车时间和公交路线。

    1. 路由名称
    2. 总线名称
    3. 停止时间 结果应仅限于从1分钟前到20分钟后的公共汽车次数。

    如果你能帮忙,请告诉我。

    更新 问题是,正如一个答案所说,我是在比较日期和时间。 我不能使用日期,因为我的值有时间,但没有日期。

     SELECT  stop_times.stop_id, stop_times.trip_id,   UNIX_TIMESTAMP(CONCAT(DATE_FORMAT(NOW(),'%Y-%m-%d '), stop_times.arrival_time)) as stopTime, routes.route_short_name, routes.route_long_name, trips.trip_headsign  FROM trips 
    JOIN stop_times ON trips.trip_id = stop_times.trip_id 
    JOIN routes ON routes.route_id = trips.route_id
    WHERE  stop_times.stop_id = 5508
    HAVING stopTime > (UNIX_TIMESTAMP(NOW()) - 60) AND stopTime < (UNIX_TIMESTAMP(NOW()) + (60*20));
    
    3 回复  |  直到 14 年前
        1
  •  1
  •   Andy    14 年前

    DATE_ADD /具有日期时间字段的子工作。确保两者类型相同。

        2
  •  1
  •   Simon S    14 年前

    请尝试以下操作:

    SELECT * FROM
    (SELECT  stop_times.stop_id, STR_TO_DATE(stop_times.arrival_time, '%H:%i:%s') as stopTime, routes.route_short_name, routes.route_long_name, trips.trip_headsign  FROM trips 
    JOIN stop_times ON trips.trip_id = stop_times.trip_id 
    JOIN routes ON routes.route_id = trips.route_id
    WHERE  stop_times.stop_id = 5508) AS qu_1
    WHERE qu_1.stopTime BETWEEN DATE_SUB(qu_1.stopTime,INTERVAL 1 MINUTE) AND  DATE_ADD(qu_1.stopTime,INTERVAL 20 MINUTE);
    

    必须警告你我还没有测试过这个,但它确实消除了HAVING子句的需要。

        3
  •  1
  •   David M    14 年前

    不要使用合成列stopTime作为输出。

    我认为你的问题应该是这样的:

    SELECT  stop_times.stop_id, STR_TO_DATE(stop_times.arrival_time, '%H:%i:%s') as stopTime, routes.route_short_name, routes.route_long_name, trips.trip_headsign  FROM trips 
    JOIN stop_times ON trips.trip_id = stop_times.trip_id 
    JOIN routes ON routes.route_id = trips.route_id
    WHERE  stop_times.stop_id = 5508
    AND arrival_time BETWEEN <something> AND <something else>
    

    HAVING 你写的子句应该总是返回true,所以我猜这不是你真正想要的。