Database MySQL查询不返回任何数据
我需要检索特定时间段的数据。 在我指定时间段之前,查询工作正常。我指定时间段的方式有问题吗?我知道在这个时间范围内有很多条目 此查询返回空值:Database MySQL查询不返回任何数据,database,mysql,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 s
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中的
查询应该提供给定公交车站的停车时间和公交路线。
对于公共汽车站,我试图获得:
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));
选择stop_times.stop_id、stop_times.trip_id、UNIX_TIMESTAMP(CONCAT(日期格式(现在(),“%Y-%m-%d”),stop_times.arrival_time))作为stopTime、routes.route_short_name、routes.route_long_name、trips.trip_headign FROM trips
在trips.trip\u id=stop\u times.trip\u id上加入停止时间
在routes.route_id=trips.route_id上加入路由
其中stop\u times.stop\u id=5508
具有stopTime>(UNIX_时间戳(NOW())-60)和stopTime<(UNIX_时间戳(NOW())+(60*20));
Stoptime是一个时间值,并且/子工作带有日期时间字段。确保它们都是相同的类型。请尝试以下方法:
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子句的需要。除了作为输出之外,不要使用合成列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>
选择stop_times.stop_id,STR_TO_DATE(stop_times.arrival_time,“%H:%i:%s”)作为stop time,routes.route_short_name,routes.route_long_name,trips.trip_headign FROM trips
在trips.trip\u id=stop\u times.trip\u id上加入停止时间
在routes.route_id=trips.route_id上加入路由
其中stop\u times.stop\u id=5508
和之间的到达时间
您编写的HAVING
子句应该总是返回true,所以我猜这不是您真正想要的