从两个表加入SQL

时间:2017-09-12 05:40:42

标签: sql sql-server tsql

我有两张具有相同结构的表格。 我想根据id显示来自两个表的非匹配记录,但它们应该显示在同一行中,如下图所示:

enter image description here

1 个答案:

答案 0 :(得分:0)

很少继续下去,但可能会像:

SELECT table1.Id, table1.Name [, table1.etc]
FROM table1 LEFT JOIN table2
  ON table1.Id = table2.id
WHERE table2.id is null
UNION 
SELECT table2.Id, table2.Name [, table2.etc]
FROM table2 LEFT JOIN table1
  ON table2.Id = table1.id
WHERE table1.id is null