【问题标题】:joining three tables keeping void fields加入三个保持空字段的表
【发布时间】:2019-09-10 15:45:49
【问题描述】:

我有三个要按 id 加入的表。第一个是主要包含属性的所有信息。其他两个对于某个 id 可能具有空值。在结果表中,我想保留三个表的所有字段,这些字段对于某些行可能是无效的。

主表

 id| attr1
----------
  1| hello
  2| hello2

第二张桌子

id| type
---------
 1| url

第三张桌子

 id| address
-------------
  2| www.example.com

我会得到的结果是

 id| attr1  | type | address
----------------------------------
  1| hello  | url  |
  2| hello2 |      | www.example.com

我检查了其他类似的帖子:Joining 3 tables Oracle SQL,但它们并没有解决我的问题。

【问题讨论】:

  • 你有没有尝试过?请展示你的尝试。

标签: mysql sql join


【解决方案1】:

你想要left joins:

select m.*, s.type, t.address
from main m left join
     second s
     on m.id = s.id left join
     third t
     on m.id = t.id;

【讨论】:

  • 最后一列“地址”仍未出现
猜你喜欢
  • 1970-01-01
  • 2017-07-19
  • 1970-01-01
  • 1970-01-01
  • 2017-07-29
  • 2019-06-04
  • 2020-08-17
  • 2016-02-04
  • 2018-03-29
相关资源
最近更新 更多