2012-03-13 230 views
5

我一直在試圖爲公共交通系統編寫一個查詢,當我輸入起點和終點站號時,它將輸出路線列表。嵌套的MySQL查詢

這裏是我的MySQL表:

mysql> desc route_timings; 
+----------------+---------+------+-----+---------+----------------+ 
| Field   | Type | Null | Key | Default | Extra   | 
+----------------+---------+------+-----+---------+----------------+ 
| ID    | int(11) | NO | PRI | NULL | auto_increment | 
| route_number | int(11) | NO |  | NULL |    | 
| stop_number | int(11) | NO |  | NULL |    | 
| arrival_time | time | YES |  | NULL |    | 
| departure_time | time | YES |  | NULL |    | 
+----------------+---------+------+-----+---------+----------------+ 
5 rows in set (0.00 sec) 

下面是樣本值:

mysql> select * from route_timings; 
+----+--------------+-------------+--------------+----------------+ 
| ID | route_number | stop_number | arrival_time | departure_time | 
+----+--------------+-------------+--------------+----------------+ 
| 1 |   54 |   1 | 10:00:00  | 10:05:00  | 
| 2 |   54 |   2 | 11:00:00  | 11:05:00  | 
| 3 |   54 |   3 | 12:00:00  | 12:05:00  | 
| 4 |   55 |   3 | 13:00:00  | 13:05:00  | 
| 5 |   55 |   4 | 14:00:00  | 14:05:00  | 
| 6 |   55 |   5 | 15:00:00  | 15:05:00  | 
| 7 |   60 |   3 | 10:00:00  | 10:05:00  | 
| 8 |   60 |   2 | 11:00:00  | 11:05:00  | 
| 9 |   60 |   1 | 12:00:00  | 12:05:00  | 
+----+--------------+-------------+--------------+----------------+ 
9 rows in set (0.01 sec) 

我的問題是:如果我想列出ROUTE_NUMBER的,將包含stop_number 1和stop_number 3,我會寫類似這樣的查詢:

SELECT DISTINCT `route_number` FROM `route_timings` WHERE `route_number` IN (
    SELECT `route_number` FROM `route_timings` WHERE `stop_number`=1 
) AND `route_number` IN (
    SELECT `route_number` FROM `route_timings` WHERE `stop_number`=3 
); 

但是,上面的查詢只會返回包含兩個站點的route_number,而不是源站點(1)在目標站點(3)之前到達的路線。

查詢將返回以下:

+--------------+ 
| route_number | 
+--------------+ 
|   54 | 
|   60 | 
+--------------+ 

的ROUTE_NUMBER 60不會從1開始,到3,而是從3開始並且前往1.是否有人可以幫助我,有點添加到查詢以便查詢僅輸出其中stop_number 1的到達時間小於stop_number 3的到達時間的route_number。

在此先感謝。 -Shain

回答

1

做了如何像重命名爲聯合收割機querys工作表約定我習慣了,但原則應該清楚

+1

這就是我一直在尋找。非常感謝你。 – 2012-03-13 09:28:14

0

用途:

SELECT DISTINCT `route_number` FROM `route_timings` WHERE `stop_number` IN (1,3) 

注:爲什麼DEPARTURE_TIME爲>比ARRIVAL_TIME?

+0

我認爲這是因爲運輸到達節點,然後很快離開 – Tung 2012-03-13 09:10:10

+0

到達時間是公交車到達公交車站的時間,departure_time是時間巴士在接客之後離開巴士站。 – 2012-03-13 09:12:31

+0

@Dax,您建議的查詢不會確保路線1和2都出現在路線中。它將返回包含這兩者中任何一個的路線。 + -------------- + | route_number | + -------------- + | 54 | | 55 | | 60 | + -------------- + – 2012-03-13 09:14:29

1
SELECT DISTINCT `route_number` R1 FROM R1.`route_timings` WHERE R1.`stop_number` =3 AND R1.`route_number` IN (
SELECT `route_number` R2 FROM `route_timings` WHERE R2.`stop_number`=1 AND R2.`arrival_time` > R1.`arrival_time` 
); 

我現在不能嘗試,但我希望語法是正確的。我所做的是選擇經過3的路線,然後檢查他們是否稍後停在1。這是使用SQL Server的語法和名稱

select fromStop.RouteNumber 
from routeTimings fromStop 
    inner join routeTimings toStop on toStop.RouteNumber = fromStop.RouteNumber and toStop.StopNumber = 3 and toStop.ArrivalTime > fromStop.DepartureTime 
where fromStop.StopNumber = 1 

注意調整爲:我通常使用SQLite,讓像我在SELECT route_number R2

+0

我不確定您是否可以在MySQL中執行此操作,因爲它會返回以下錯誤:錯誤1146(42S02):表'R1.route_timings'doesn '不存在'。 – 2012-03-13 09:22:02

+0

我剛剛發現這個作品,如果你刪除所有的引號 – Erpheus 2012-03-15 11:22:00