不存在匹配条目时如何联接两个表

时间:2019-12-10 06:20:11

标签: mysql sql

我在编写此SQL查询时遇到麻烦。

基本上,我有下表;

received_flight_files_table & uploaded_flight_files_table

以及以下字段;

Processing_Month, IATA_Code, Airline, Received_Date, Uploaded_Date

我希望查询产生类似于第一个附件的屏幕截图。

EXPECTED OUTPUT

您可以看到的2个日期字段来自不同的表。 可以在IATA_Code和Airline字段上进行合并,因为这两个表中的这两个值应相同。

这里有两个我写的陈述不正确的例子;

SELECT DISTINCT received_flight_files_table.Processing_Month, received_flight_files_table.IATA_Code, received_flight_files_table.Airline, received_flight_files_table.Received_Date, uploaded_flight_files_table.Uploaded_Date, published_flight_table.Published_Date
FROM ((received_flight_files_table 
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.Processing_Month=received_flight_files_table.Processing_Month) 
INNER JOIN published_flight_table ON published_flight_table.Published_Month=received_flight_files_table.Processing_Month
WHERE received_flight_files_table.Processing_Month = [enter MMMYY];`   

SELECT DISTINCT received_flight_files_table.Processing_Month, received_flight_files_table.IATA_Code, received_flight_files_table.Airline, received_flight_files_table.Received_Date, uploaded_flight_files_table.Uploaded_Date
FROM (((received_flight_files_table 
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.Processing_Month=received_flight_files_table.Processing_Month
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.IATA_Code=received_flight_files_table.IATA_Code
INNER JOIN uploaded_flight_files_table ON uploaded_flight_files_table.Airline=received_flight_files_table.Airline)))
WHERE (received_flight_files_table.Processing_Month = [enter MMMYY];

例如,在Uploaded_Date字段中有时应该有一些空白-在一张表中,我可能已经收到文件,但尚未上传。

1 个答案:

答案 0 :(得分:0)

尝试使用LEFT JOIN代替INNER JOIN

https://www.diffen.com/difference/Inner_Join_vs_Outer_Join