Sql 查询以从多个表中获取键和列
所需输出Sql 查询以从多个表中获取键和列,sql,sql-server-2008,tsql,Sql,Sql Server 2008,Tsql,所需输出 Table1 id1_a id2_a id3_a col 1 col2 col3 1 -1 3 a b c Table2 id1_b id2_b id3_b col 4 col5 col6 4 6 2005 d e f
Table1
id1_a id2_a id3_a col 1 col2 col3
1 -1 3 a b c
Table2
id1_b id2_b id3_b col 4 col5 col6
4 6 2005 d e f
需要一个查询来获得如图所示的多个表数据的输出吗?作为占位符,当您了解这些链接
------
key col1 col2 col3 col 4 col5 col6
1/-1/3 a b c null null nul
4/6/2005 null null null d e f
我想OP想要这个
SELECT (CONCAT(t1.id1_a,'/',t1.id2_a,'/',t1.id3_a) AS Key1,
(CONCAT(t2.id1_b,'/',t2.id2_b,'/',t2.id3_b) AS Key2,
t1.col1, t1.col2, t1.col3, t2.col4, t2.col5, t2.col6
FROM Table1 t1
INNER JOIN Table2 t2 ON t1.? = t2.?
您需要为键列设置单独的列,例如,Key1 key2也可以以任何方式链接表吗?谢谢,但我只需要一个键,而不是Key1和key2。是的,一个
UNION
是一种方法,我在一个查询中得到结果时非常盲目
Select (id1_a + ' ' +id2_a + ' ' +id3_a) as key, col1 , col2 , col3 ,
null as col4 , null as col5 , null as col6 from Table1
union
Select (id1_b + ' ' +id2_b + ' ' +id3_b) as key, null as col1 , null as col2 ,
null as col3 , col4 , col5 , col6 from Table2