【问题标题】:postgresql: several tables in from statementpostgresql:from语句中的几个表
【发布时间】:2021-02-06 07:45:42
【问题描述】:

我见过这样的查询:

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 部分 - 它是用于完全连接的某种语法糖吗?

【问题讨论】:

  • 这是旧的加入方式(inner join),一般不建议这样做

标签: sql postgresql subquery inner-join where-clause


【解决方案1】:

这些是隐式内部连接。表名在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
)

【讨论】:

    猜你喜欢
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    • 2014-06-17
    • 1970-01-01
    • 2016-12-21
    相关资源
    最近更新 更多