[英]"merge" two tables in SQL
I have two tables - tbl1,tbl2.我有两个表 - tbl1,tbl2。
tbl1表1
(id int, name string):
(1, dan),
(2, shon),
(3, mimi),
(4, mali).
tbl2表 2
(int id, second_name string):
(1, shiri),
(3, mili).
I want to do a "join" using the id but I don't want the ids that are in tbl1 and no in tbl 2 to disappear.我想使用 id 进行“加入”,但我不希望 tbl1 中的 id 和 tbl 2 中没有的 id 消失。
If I will do join the output will be:如果我加入 output 将是:
(1, dan, shiri),
(3, mimi, mili).
but the output that I need is:但我需要的 output 是:
(1, dan, shiri),
(2, shon, *blank/null*),
(3, mimi, mili),
(4, mali, *blank/null*).
Please help me!请帮我!
It sounds like you want a LEFT OUTER JOIN
.听起来您想要一个
LEFT OUTER JOIN
。 Assuming you are using SQL Server, it would be like this:假设你使用的是 SQL 服务器,它会是这样的:
SELECT tbl1.id, tbl1.name, tbl2.second_name
FROM tbl1
LEFT OUTER JOIN tbl2 ON tbl1.id = tbl2.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.