选择查询3个联接表

时间:2018-12-06 13:43:50

标签: mysql sql join select

我有2张桌子,桌子飞行和桌子离开都有

Table flight:

flightnr|   dest    |   start
        |           |
--------------------------------
        |           |
LH100   |   ATL     |   FRA
LH102   |   DXB     |   FRA
LH103   |   ORD     |   FRA
LH104   |   HND     |   FRA
LH105   |   LHR     |   FRA
LH106   |   LAX     |   FRA
LH107   |   HKG     |   FRA
LH108   |   CDG     |   FRA
LH109   |   DFW     |   FRA
LH110   |   IST     |   FRA
LH200   |   ATL     |   LHR
LH201   |   PEK     |   LHR
LH202   |   DXB     |   LHR
LH203   |   ORD     |   LHR
LH204   |   FRA     |   LHR

另一个表是

Table departure:

flightnr|   date        |   sign
        |               |
---------------------------------------
        |               |
LH-100  |   2018-01-10  |   D-ABBL
LH-100  |   2018-02-10  |   D-ABBL
LH-100  |   2018-03-10  |   D-ABBL
LH-100  |   2018-04-10  |   D-ABBL
LH-100  |   2018-05-10  |   D-ABBL
LH-100  |   2018-06-10  |   D-ABBL
LH-100  |   2018-07-10  |   D-ABBK
LH-100  |   2018-08-10  |   D-ABBK
LH-102  |   2018-02-10  |   D-ABBB
LH-102  |   2018-04-10  |   D-ABBB
LH-102  |   2018-06-10  |   D-ABBB
LH-103  |   2018-09-10  |   D-ABBB
LH-104  |   2018-03-10  |   D-ABBB
LH-104  |   2018-05-10  |   D-ABBB
LH-104  |   2018-07-10  |   D-ABBB
LH-104  |   2018-09-10  |   D-ABBB
LH-105  |   2018-01-10  |   D-ABBB
LH-105  |   2018-02-10  |   D-ABBB
LH-105  |   2018-03-10  |   D-ABBB
LH-200  |   2018-01-10  |   D-ABCA
LH-200  |   2018-02-10  |   D-ABCA
LH-201  |   2018-03-10  |   D-ABCA
LH-201  |   2018-05-10  |   D-ABBR
LH-202  |   2018-05-10  |   D-ABCA
LH-202  |   2018-09-10  |   D-ABCA
LH-203  |   2018-04-10  |   D-ABCA
LH-203  |   2018-05-10  |   D-ABCA
LH-203  |   2018-06-10  |   D-ABCC
LH-204  |   2018-01-10  |   D-ABBF
LH-204  |   2018-03-10  |   D-ABBI
LH-204  |   2018-08-10  |   D-ABBM

所以现在我要选择使我有可能从iata'FRA'飞往'PEK'的航线,这是我无法在1班次中起飞的,因为没有从'FRA'到'PEK'的直接航班在桌子出发。

作为客户,我必须先从“ FRA”飞到“ LHR”,然后才能从“ LHR”飞到“ PEK”。

我现在的问题是,我真的不知道如何选择能使我从“ FRA”飞到“ PEK”的线。输出应该是这样的:

   date    | flightnr | start | change_over_dest |   date    | flightnr | change_over_start | dest
------------+--------+-------+--------------+------------+--------+---------------+------
 2018-03-10| LH-105   | FRA   | LHR              | 2018-03-10| LH-201   | LHR               | PEK
 2018-03-10| LH-105   | FRA   | LHR              | 2018-05-10| LH-201   | LHR               | PEK

我已经尝试了在两个表之间使用JOINS的许多不同版本(使用2个JOINS加入到自身的航班,然后出发),但是它们都没有以正确的方式起作用。

希望我以清楚的方式描述了我的问题,你们可以帮助我。

问候

Zeroza

2 个答案:

答案 0 :(得分:0)

我们可以尝试在flight表上进行自联接,条件是第一个目的地是第二条路线的起点。此外,我们可以限制起始和最终目的地城市。请注意,我们还将flight表加入到每个departure表中,以输入出发日期。

SELECT
    d1.date AS date_start,
    f1.flightnr AS flightnr_start,
    f1.start AS first_start,
    f1.dest AS change_over_dest,
    d2.date AS date_end,
    f2.flightnr AS flightnr_end,
    f2.start AS last_start,
    f2.dest AS change_over_end
FROM flight f1
INNER JOIN departure d1
    ON f1.flightnr = d1.flightnr
INNER JOIN flight f2
    ON f1.dest = f2.start
INNER JOIN departure d2
    ON f2.flightnr = d2.flightnr AND d1.date = d2.date
WHERE
    f1.start = 'FRA' AND f2.dest = 'PEK';

我没有打扰围绕您的完整样本数据集进行演示,因为它充满了错别字。相反,我只是在下面设置了一个简单的演示,它似乎正在工作。

Demo

答案 1 :(得分:0)

DROP TABLE IF EXISTS fight;

CREATE TABLE flight
(flightnr CHAR(5) NOT NULL PRIMARY KEY
,destination CHAR(3) NOT NULL
,start CHAR(3) NOT NULL
);

INSERT INTO flight VALUES
('LH100','ATL','FRA'),
('LH102','DXB','FRA'),
('LH103','ORD','FRA'),
('LH104','HND','FRA'),
('LH105','LHR','FRA'),
('LH106','LAX','FRA'),
('LH107','HKG','FRA'),
('LH108','CDG','FRA'),
('LH109','DFW','FRA'),
('LH110','IST','FRA'),
('LH200','ATL','LHR'),
('LH201','PEK','LHR'),
('LH202','DXB','LHR'),
('LH203','ORD','LHR'),
('LH204','FRA','LHR');

DROP TABLE IF EXISTS departure;

CREATE TABLE departure
(flightnr CHAR(5) NOT NULL
,date DATE NOT NULL
,sign CHAR(6) NOT NULL
,PRIMARY KEY(flightnr,date)
);

INSERT INTO departure VALUES
('LH100','2018-10-01','D-ABBL'),
('LH100','2018-10-02','D-ABBL'),
('LH100','2018-10-03','D-ABBL'),
('LH100','2018-10-04','D-ABBL'),
('LH100','2018-10-05','D-ABBL'),
('LH100','2018-10-06','D-ABBL'),
('LH100','2018-10-07','D-ABBK'),
('LH100','2018-10-08','D-ABBK'),
('LH102','2018-10-02','D-ABBB'),
('LH102','2018-10-04','D-ABBB'),
('LH102','2018-10-06','D-ABBB'),
('LH103','2018-10-09','D-ABBB'),
('LH104','2018-10-03','D-ABBB'),
('LH104','2018-10-05','D-ABBB'),
('LH104','2018-10-07','D-ABBB'),
('LH104','2018-10-09','D-ABBB'),
('LH105','2018-10-01','D-ABBB'),
('LH105','2018-10-02','D-ABBB'),
('LH105','2018-10-03','D-ABBB'),
('LH200','2018-10-01','D-ABCA'),
('LH200','2018-10-02','D-ABCA'),
('LH201','2018-10-03','D-ABCA'),
('LH201','2018-10-05','D-ABBR'),
('LH202','2018-10-05','D-ABCA'),
('LH202','2018-10-09','D-ABCA'),
('LH203','2018-10-04','D-ABCA'),
('LH203','2018-10-05','D-ABCA'),
('LH203','2018-10-06','D-ABCC'),
('LH204','2018-10-01','D-ABBF'),
('LH204','2018-10-03','D-ABBI'),
('LH204','2018-10-08','D-ABBM');

SELECT f1.flightnr
     , f1.start
     , d1.date startdate
     , f2.flightnr transferflightnr
     , f2.start transferlocation
     , d2.date transferdate
     , f2.destination 
  FROM flight f1 
  JOIN departure d1 
    ON d1.flightnr = f1.flightnr 
  JOIN flight f2 
    ON f2.start = f1.destination 
  JOIN departure d2 
    ON d2.flightnr = f2.flightnr 
   AND d2.date >= d1.date 
 WHERE f2.destination = 'pek' 
   AND f1.start = 'fra';
+----------+-------+------------+------------------+------------------+--------------+-------------+
| flightnr | start | startdate  | transferflightnr | transferlocation | transferdate | destination |
+----------+-------+------------+------------------+------------------+--------------+-------------+
| LH105    | FRA   | 2018-10-01 | LH201            | LHR              | 2018-10-03   | PEK         |
| LH105    | FRA   | 2018-10-02 | LH201            | LHR              | 2018-10-03   | PEK         |
| LH105    | FRA   | 2018-10-03 | LH201            | LHR              | 2018-10-03   | PEK         |
| LH105    | FRA   | 2018-10-01 | LH201            | LHR              | 2018-10-05   | PEK         |
| LH105    | FRA   | 2018-10-02 | LH201            | LHR              | 2018-10-05   | PEK         |
| LH105    | FRA   | 2018-10-03 | LH201            | LHR              | 2018-10-05   | PEK         |
+----------+-------+------------+------------------+------------------+--------------+-------------+