【问题标题】:Select from 2 tables. Query = table1 OR table1 + table2从 2 个表中选择。查询 = 表 1 或表 1 + 表 2
【发布时间】:2014-03-12 17:54:55
【问题描述】:

我有 2 个具有 1:1 关系的表。 我正在尝试使用 table1 中的 PK 从 table1 和 table2 中选择所有内容。

我想从 table1 中选择所有内容,并且 IF table1 PK = table2 FK THEN(并且只有 THEN)从 table2 中选择。

简而言之,即使查询结果在 table2 中没有任何关系,我也想从 table1 中进行选择。如果有,那么也从 table2 中选择。

有人知道怎么做吗?

【问题讨论】:

  • 谢谢你!它奏效了。

标签: select if-statement exists


【解决方案1】:

左外连接

select table1.*, table2.*
from table1 left outer join table2 on table1.PF = table2.PK

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 2023-03-26
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多