在PostgreSQL中过滤条件为空时从表中获取空列

问题描述 投票:0回答:1

我有两张桌子table1(id,name,type)table2(id,source,destination)

当我运行查询

SELECT
    name,
    source,
    destination
FROM
    table1,
    table2
WHERE
    table1.id=table2.id

如果两个表之间没有id匹配,我仍然可以获得源列和目标的空列。

postgresql filter where
1个回答
1
投票

是的,你基本上想要一个OUTER JOIN并且记得总是使用显式的ANSI JOIN语法,而不是连接的隐式逗号语法。还要使用适当的表别名来避免歧义。

SELECT
    t1.name,
    t2.source,
    t2.destination
FROM
    table1 t1 left outer join
    table2 t2 ON t1.id = t2.id
© www.soinside.com 2019 - 2024. All rights reserved.