2014-11-21 42 views
0

我有以下任务:SQL创建视图连接航班

我需要创建一个视图,显示第1次航班和他的连接航班。

在这个例子中FlightNo 1 & 2.

-

我的飞行表:

FlightNo---Date--------StartTime---ArrivingTime---StartPort---DestinationPort 

1. ----2014-11-20---01:00:55-----02:00:34----------a----------------b 
2. ----2014-11-20---02:10:55-----03:00:34----------b----------------c 
3. ----2014-11-20---20:00:55-----21:00:34----------x----------------q 
4. ----2014-11-20---00:00:55-----01:00:34----------a----------------u 

...

到目前为止我的代码:

create OR REPLACE view FlightConnection as 

select* FROM Flight a 

where exists (select* FROM Flight b 
where a.StartPort = b.DestinationPort and 
a.ArrivingTime < b.StartTimet); 

我的输出:

FlightNo---Date--------StartTime---ArrivingTime---StartPort---DestinationPort 

---1 ----2014-11-20---01:00:55-----02:00:34----------a----------------b 

-

但我需要FlightNo 1和2 我尝试了几种不同的事情,但我想我暂时哑。

请帮忙。 (对不起,我的英语)

+0

所以,你要对航班,使得第一的目的端口是第二的起始端口,和第一的到达时间是第二次的起飞时间前? – 2014-11-21 18:32:27

回答

0

您需要执行(自)JOIN以获取包含有关可能连接的两个航班的数据的结果行。这应该让你开始:

CREATE OR REPLACE VIEW FlightConnection AS 
    SELECT * 
    FROM Flight a 
    JOIN Flight b 
     ON a.DestinationPort = b.StartPort 
     AND a.Date = b.Date 
     AND a.ArrivingTime < b.StartTime 
+0

您可能想要选择特定列并为其分配别名,而不是只选择'*'。 – 2014-11-21 18:39:52

+0

是的,somthing现在不能正常工作mysql正在为错误而哭泣1060重复的列名FlightNo – 2014-11-21 18:58:30

+0

选择选择列表中的特定列并为其分配不同的别名,或者在'AS'之前放置适当的列列表。或两者。 – 2014-11-21 19:06:57