【问题标题】:How to join two table one of them has duplicate of ID如何加入两个表其中一个有重复的ID
【发布时间】:2021-10-14 10:14:06
【问题描述】:

我有两张表,其中一张有重复的 ID,其中一张有不同的 ID。我很难加入这些表以获得所需的结果,如下所示: 表一

id Country City
1 USA Boston
1 USA Detroit
2 Mexico Mexico City
2 Mexico Tijuana
3 Canada Toronto
3 Canada Vancouver

表 2

id Country Status
1 USA Open
2 Mexico Closed
3 Canada Open

我想得到如下结果:

id Country City Status
1 USA Boston Open
1 USA Detroit Open
2 Mexico Mexico City Closed
2 Mexico Tijuana Closed
3 Canada Toronto Open
3 Canada Vancouver Open

【问题讨论】:

  • 这看起来像一个非常简单的JOIN。您尝试了什么,为什么没有成功?
  • 看起来表 1 有多余的列。它不需要idCountry,因为它们复制了Table 2 中的内容。

标签: mysql sql join


【解决方案1】:

你试过基本的JOIN吗?

select t1.*, t2.status
from table1 t1 join
     table2 t2
     on t1.id = t2.id;

【讨论】:

    猜你喜欢
    • 2014-09-30
    • 2014-10-08
    • 1970-01-01
    • 2022-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多