存在函数
Exists function
我正在尝试回答以下问题
编写一个查询,显示所有航班的航班号 (flno)、出发地和目的地,其中存在从目的地返回出发地的另一航班。
select distinct flno, origin, destination as d from flight
where exists (select flno, destination from flight where origin = d)
前两个答案是正确的,但它给了我更多与问题无关的答案?
航班Table:
您需要将exists
条件中的子查询与外部查询相关联。你看起来的逻辑似乎是:
select flno, origin, destination
from flight f
where exists (
select 1
from flight f1
where f1.origin = f.destination and f1.destination = f.origin
)
这里有一个不使用子查询来解决上述查询的更简单的方法:
select flno, origin, destination from flight f1, flight f2
where fl.origin = f2.destination and f1.destination = f2.origin
我正在尝试回答以下问题
编写一个查询,显示所有航班的航班号 (flno)、出发地和目的地,其中存在从目的地返回出发地的另一航班。
select distinct flno, origin, destination as d from flight
where exists (select flno, destination from flight where origin = d)
前两个答案是正确的,但它给了我更多与问题无关的答案?
航班Table:
您需要将exists
条件中的子查询与外部查询相关联。你看起来的逻辑似乎是:
select flno, origin, destination
from flight f
where exists (
select 1
from flight f1
where f1.origin = f.destination and f1.destination = f.origin
)
这里有一个不使用子查询来解决上述查询的更简单的方法:
select flno, origin, destination from flight f1, flight f2
where fl.origin = f2.destination and f1.destination = f2.origin