2017-04-22 46 views
-1

我做了一個查詢,希望沒有任何重複,但我有一些3次重複,當我使用DISTINCT或DISTINCTROW時,我只有2個重複。訪問沒有重複結果的SQL查詢

SELECT f.flight_code, 
     f.status, 
     a.airport_name, 
     a1.airport_name, 
     f.departing_date+f.departing_time AS SupposedDepartingTime, 
     f.landing_date+f.landing_time AS SupposedLandingTime, 
     de.actual_takeoff_date+de.actual_takeoff_time AS ActualDepartingTime, 
     SupposedLandingTime+(ActualDepartingTime-SupposedDepartingTime) AS ActualLandingTime 
FROM 
(((Flights AS f 
    LEFT JOIN Aireports AS a 
     ON a.airport_code = f.depart_ap) 
    LEFT JOIN Aireports AS a1 
     ON f.target_ap = a1.airport_code) 
    LEFT JOIN Irregular_Events AS ie 
     ON f.flight_code = ie.flight_code) 
    LEFT JOIN Delay_Event AS de 
     ON ie.IE_code = de.delay_code; 

不得不使用LEFT JOIN,因爲當我用INNER JOIN我錯過了一些我想表現的東西,因爲我想看到所有的航班,不僅如此被推延或取消航班。

這是我使用INNER JOIN時的結果,只能看到狀態爲「ביטול」或「עיכוב」的航班,這不是我想要的。

the results with LEFT JOIN ,當我使用DISTINCT,你看到第一列6號的行是隻出現兩次

重要! 我剛查過我的查詢和我在那裏使用的所有表格,發現我的問題,但不知道如何修復它! 在表Irregular_Events我有更多的航班3,6和8的一個事件,這就是爲什麼當我使用左加入我看到更多,即使你我使用不同,請給我一些幫助!

+1

使用'SELECT DISTINCT'。 –

+1

請問你讀過嗎?我寫了我用它,但它不是很好。 那麼如果我在下次回答之前沒有在代碼中展示它,那麼該怎麼辦呢? –

+3

好戰的態度不會增加某人提供和回答的可能性。 – lit

回答

0

很難說出沒有任何輸出樣本的查詢有什麼問題,也沒有任何關於表結構的描述。

但是,你的問題是,你正在查詢的flights表,[我認爲]可以鏈接到多個irregular_events,它也可能鏈接到多個delay_event

如果你想每個航班只得到一行,你需要確保你的連接只返回一行。也許你可以通過向連接添加一個更多的條件,或者通過在子查詢中添加一個條件來完成。

編輯

你可以嘗試將GROUP BY添加到查詢:

GROUP BY 
     f.flight_code, 
     f.status, 
     a.airport_name, 
     a1.airport_name; 
+0

航班可以鏈接到多個'irregular_events',但'irregular_events'可以鏈接到每個'delay_event'。 通過使用'irregular_events'和'delay_event'我可以知道一個航班是取消或只是延遲,然後我可以顯示每個航班的實際起飛和着陸時間。 但如果我使用INNER JOIN我只看到延遲和取消的航班,這不是我想要的,所以我使用了LEFT JOIN,然後我在某些地方得到了3個副本 –

+0

@DoronTayar看到我的編輯。 –

1

不能完全確定,沒有看到表結構,但是這可能工作:

SELECT f.flight_code, 
     f.status, 
     a.airport_name, 
     a1.airport_name, 
     f.departing_date+f.departing_time AS SupposedDepartingTime, 
     f.landing_date+f.landing_time AS SupposedLandingTime, 
     de.actual_takeoff_date+de.actual_takeoff_time AS ActualDepartingTime, 
     SupposedLandingTime+(ActualDepartingTime-SupposedDepartingTime) AS ActualLandingTime 
FROM 
((Flights AS f 
    LEFT JOIN Aireports AS a 
     ON a.airport_code = f.depart_ap) 
    LEFT JOIN Aireports AS a1 
     ON f.target_ap = a1.airport_code) 
    LEFT JOIN 
    (
    SELECT 
    ie.flight_code, 
    de1.actual_takeoff_date, 
    de1.actual_takeoff_time 
    FROM 
    Irregular_Events ie 
    INNER JOIN Event AS de1 
     ON ie.IE_code = de1.delay_code 
    ) AS de 
     ON f.flight_code = de.flight_code