如何将table2的1 col中的所有数据连接到table1,同时将不匹配的数据保留在table1中 [英] How do i join all data in 1 col of a table2 to table1 whilst keeping non matching data in table1

查看:92
本文介绍了如何将table2的1 col中的所有数据连接到table1,同时将不匹配的数据保留在table1中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将信息从表2连接到我的表1,而不会丢失表1中的任何数据.我尝试了内部,完全外部,左右连接,但都带来了相同的结果-删除了一些table1数据行.

I am trying to join information to my table 1 from table 2 without losing any data in table1. I have tried inner, full outer, left and right joins but all bring same result - removing some table1 data rows.

表1

Col1, Col2, col3
1, mary, null
2, john, null
3, sally, 25
4, barry, 15

表2

Col1, Col2
2, fireman, 1
3, office, 2
4, teacher, 3

期望的结果

col1, col2, col3, col4
1, mary, null, null
2, john, null, fireman
3, sally, 25, office
4, barry, 15, teacher

我能得到什么;

2, john, null, fireman
3, sally, 25, office
4, barry, 15, teacher

关于我的查询

Select col1, col2, col3 from table1 left join col2 on table1.col1=table2.col1 order by table2.col3 asc

推荐答案

SELECT   T1.col1, 
         T1.col2, 
         T1.col3, 
         T2.col2 AS col4
FROM     Table1 T1
         LEFT JOIN Table2 T2
             ON T1.col1 = T2.col1 
ORDER BY T2.col2 ASC

小提琴演示

这篇关于如何将table2的1 col中的所有数据连接到table1,同时将不匹配的数据保留在table1中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆