连接3个表只能获得两行

时间:2012-03-22 12:01:51

标签: sql sql-server-2008

我有三个表我需要查询表1,2和3并分别从表1中选择C1,从表2和3中选择C2,以便它给出结果。有没有办法得到这个? enter image description here

2 个答案:

答案 0 :(得分:4)

是:

select t1.c1, coalesce(t2.c2, t3.c2) c2
from Table1 t1
left join Table2 t2 on t1.c1 = t2.c1
left join Table3 t3 on t1.c1 = t3.c1

答案 1 :(得分:1)

尝试以下方法:

SELECT T1.C1, T2.C2
FROM T1
INNER JOIN T1.C1 = T2.C1
UNION ALL
SELECT T1.C1, T3.C2
FROM T1
INNER JOIN T1.C1 = T3.C1