MySQL查询不返回任何数据

时间:2010-05-03 18:01:48

标签: database mysql

我需要从特定时间段检索数据。 查询工作正常,直到我指定时间段。我指定时间段的方式有问题吗?我知道在那段时间内有很多条目。

此查询返回空:

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)

如果删除HAVING子句(不指定时间范围),查询将起作用。返回:

+---------+----------+------------------+-----------------+---------------+
| 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. 巴士方向(标题)
  4. 停止时间 结果应仅限于从1分钟前到20分钟的公交车时间。
  5. 如果你能提供帮助,请告诉我。

    更新 一个答案说,问题是我正在将DATE与DATETIME进行比较。 我无法使用DATE,因为我的值有时间而不是日期。 所以我的解决方案是使用Unix时间:

     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 个答案:

答案 0 :(得分:1)

停止时间是时间值,DATE_ADD / SUB使用日期时间字段。确保它们都是同一类型。

答案 1 :(得分:1)

请改为尝试:

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子句。

答案 2 :(得分:1)

除了作为输出之外,不要使用合成列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,所以我猜这不是你真正想到的。

相关问题