简体   繁体   中英

SQL Server : merge 2 tables

I have a table ONE with data:

Id      v1   V2   
-----------------
1       10   100     
2       15   150     
3       20   200     

and a table TWO with data:

Id     v3   V4   
------------------
1    1000    1000     
2    1500   15000     
3    2000   20000  
4     800   30000

I want to get this result:

Id      v1   V2     v3      V4
----------------------------------
1       10   100    1000    1000   
2       15   150    1500   15000   
3       20   200    2000   20000 
4       null null    800   30000

I tried this SQL code:

SELECT *
FROM [One]
FULL OUTER JOIN [Two] ON [One].ID = [Two].ID  

I might be missing something but I think you just need left join

SELECT * 
FROM [TWO] t
LEFT JOIN [ONE] o ON o.Id = t.Id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM