postgresql: from 语句中的几个表

postgresql: several tables in from statement

我见过这样的查询:

SELECT a.city
FROM airports a
EXCEPT
SELECT arr.city
FROM airports dep, airports arr, flights f
WHERE dep.city = 'Москва'
AND f.departure_airport = dep.airport_code
AND f.arrival_airport = arr.airport_code;

我不明白部分 FROM airports dep, airports arr, flights f - 它是完全连接的某种语法糖吗?

这些是隐式 内部联接。 table 名称在from 子句中枚举,连接条件放在where 子句中。

使用标准的显式连接可以更简单地表达:

SELECT arr.city
FROM airports dep
INNER JOIN airports arr ON f.arrival_airport = arr.airport_code
INNER JOIN flights f ON f.departure_airport = dep.airport_code
WHERE dep.city = 'Москва'

旁注:据我了解您的问题,您可以使用 not exists 而不是 except:

来表达整个查询
select a.city
from airports a
where not exists (
    select 1
    from flights f
    inner join airports adep on f.departure_airport = adep.airport_code
    where adep.city = 'Москва' and f.arrival_airport = a.airport_code
)