2017-04-22 49 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