存在功能

问题描述

我正在尝试回答以下查询

编写一个查询,显示所有航班的航班号(flno),始发地和目的地,其中存在另一个从目的地返回到始发地的航班。

select distinct flno,origin,destination as d from flight 
where exists (select flno,destination from flight where origin = d)

前两个答案是正确的,但是它给了我更多与问题无关的答案?

航班表:

enter image description here

解决方法

您需要将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,destination from flight f1,flight f2 
where fl.origin = f2.destination and f1.destination = f2.origin

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...