2010-05-03 148 views
2

我需要檢索特定時間段的數據。 查詢工作正常,直到我指定時間段。我指定時間段的方式有什麼問題嗎?我知道在這個時間範圍內有很多條目。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. 總線方向(HEADSIGN)
  4. 停止時間 結果應該只有1只限於公交車時間分鐘前至20分鐘。

請讓我知道,如果你可以幫助。

UPDATE 問題是我比較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)); 

回答

1

停止時間是一個時間值,並與日期時間字段DATE_ADD/SUB工作。確保它們都是相同的類型。

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子句的需要。

+0

仍爲空集。 – 2010-05-03 18:18:46

1

請勿使用除輸出以外的合成列停止時間。

我覺得您的查詢應該是這樣的:

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> 

你寫的應該總是返回true的HAVING條款,所以我猜這不是你真正的初衷是什麼。