我需要从特定的时间段检索数据。在指定时间段之前,查询工作正常。我指定时间段的方式有什么问题吗?我知道在那个时间范围内有很多条目。
此查询返回空:
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" |我正在使用加载到MySQL中的MySQL。
查询应该为给定的巴士站提供停车时间和巴士路线。对于公共汽车站,我试着得到:
如果你能帮忙,请告诉我。
UPDATE问题是,正如一个答案所说,我把日期和日期进行了比较。我不能使用日期,因为我的值有时间,但没有日期。所以我的解决方案是使用Unix time:
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));发布于 2010-05-03 18:07:21
停止时间是一个时间值,添加/SUB使用datetime字段。确保它们都是相同的类型。
发布于 2010-05-03 18:09:07
试一试:
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子句的需要。
发布于 2010-05-03 18:13:33
除了作为输出外,不要使用合成列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,所以我猜这不是您真正想要的。
https://stackoverflow.com/questions/2760116
复制相似问题