我有两张桌子加入。这里我必须在第二个表有记录集时获取映射记录。但是当第二个表没有记录时,我需要第一个表中的所有记录。
Create Table #temp1
(Id1 int)
Create Table #temp2
(Id2 int)
Insert into #temp1 Values(1),(2),(3),(4)
Insert into #temp2 Values(1),(2)
Select * from #temp1 A
Inner Join #temp2 B On A.Id1=B.Id2
当我在第二张表中有记录时,它会提供正确的输出。
但是当我在第二张表中没有记录时,我需要从第一张表中获取所有记录。
Delete #temp2
Select * from #temp1 A
Inner Join #temp2 B On A.Id1=B.Id2
此查询返回没有我尝试使用Left Outer Join的记录它给出了所有记录,但我没有得到像第一个场景的记录。
Drop Table #temp1
Drop Table #temp2
我需要这样的输出。
先谢谢。 如果问题不明确,请问我。
答案 0 :(得分:1)
如果您只想要第一张表中的列:
union all
如果您想要第二个表中的额外列,可以使用Select a.*, b.*
from #temp1 a Inner Join
#temp2 b
On a.Id1 = b.Id2
union all
select a.*, b.*
from #temp1 a left join
#temp2 b
on a.id1 = b.id2
where not exists (select 1 from #temp2);
:
library(data.table)
fread("filename.csv",nrows=5)
答案 1 :(得分:0)
实际上你可以使用OUTER JOIN
:
SELECT Id1 FROM #temp1 t1
LEFT OUTER JOIN #temp2 t2
ON t1.Id1 = t2.Id2
答案 2 :(得分:0)