【问题标题】:SQL Join Tables simple approach needed需要 SQL 连接表的简单方法
【发布时间】:2021-10-20 19:38:04
【问题描述】:

我正在寻找一个简单的逻辑来合并两个表,预期结果如下。

是否可以使用 SQL join 或 concatenate 命令来实现?

我怎样才能实现它?

这是我正在研究并试图找到解决方案的逻辑:

表 1:

ID  Name        Title               Age
2   Paul        Technical Support   22
4   Janne       IT Specialist       27
1   Wladimir    Team Lead           31
3   Mark        Customer Support    40

表 2:

ID  Name        Title               Age
2   Paul        Technical Support   22
4   Janne       IT Specialist       
1   Wladimir                        31
3   Mark        Customer Support    40

合并后的预期结果:

ID  Name        Title               Age
2   Paul        Technical Support   22
4   Janne       IT Specialist       27
1   Wladimir    Team Lead           31
3   Mark        Customer Support    40

【问题讨论】:

  • 加入表格并使用nvlisnull 或类似函数获取非缺失值,具体取决于您使用的RDBMS
  • 合并后的结果和table1一样。似乎根本不需要合并。你的问题也应该有一个适当的数据库标签。

标签: sql join merge concatenation outer-join


【解决方案1】:

您应该为此使用以下查询

select t1.id,t1.name,t1.title,t1.age,t2.id,t2.name.t2.title,t2.age from table1 t1 join table2 t2 where t1.id=t2.id

【讨论】:

  • 合并时会因为表 2 中的空白值而重复 id 4 和 1 的行?
  • 嘿,抱歉,尝试在您的 t1.id 中使用 distinct
  • 我认为,加入后,列的最大值分组可能会有所帮助。而不是不同的。
猜你喜欢
  • 2018-09-05
  • 2015-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-07
  • 1970-01-01
  • 2015-10-01
  • 1970-01-01
相关资源
最近更新 更多