加入不相关的表格

时间:2012-05-22 03:20:59

标签: sql-server

我想加入这两个不相关的表格:

表1

id   name   age
--   ----   ---
1    a      9
2    b      11
3    c      10

表2

id   school   address
--   ------   -------
1    aa       abc
1    aa       efg
3    bb       hij

期望的结果:

id   school   address   age   name
--   ------   -------   ---   ---- 
1    aa       abc       9     a
1    aa       efg       9     a
2    NULL     NULL      11    b
3    bb       hij       10    c

1 个答案:

答案 0 :(得分:1)

SELECT t1.id, t2.school, t2.address, t1.age, t1.name
FROM dbo.table1 AS t1
LEFT OUTER JOIN dbo.table2 AS t2
ON t1.id = t2.id
ORDER BY t1.id;