如何加入空白表

时间:2015-06-12 13:20:06

标签: sql sql-server sql-server-2008

我想从3个表中获取所有行,其中一行是空的。

表1:

Names  Numbers
Hello  123
Bye    123

表2:

Names  Numbers
Xyz.   246

表3:

Names  Numbers

表3不包含任何值

我希望结果包含所有这样的行:

Names  Numbers
Xyz.   246
Hello  123
Bye    123

3 个答案:

答案 0 :(得分:2)

我认为您正在寻找 Union all

select * from table1
union all
select * from table2
union all
select * from table3

加入用于合并列,而合并用于合并行

答案 1 :(得分:0)

要获得所有这些值,您必须...获取所有记录:)

SELECT Name, Numbers FROM Table1
UNION 
SELECT Name, Numbers FROM Table2
UNION 
SELECT Name, Numbers FROM Table3

答案 2 :(得分:0)

  1. 使用UNION获取明确的名单
  2. 使用LEFT JOIN检索您的数据
  3. 示例:

    SELECT list.names, t1.assigned, t2.unsassigned, t3.closed, t4.open
    FROM 
       (select names from table1
        union select names from table2
        union select names from table3
        union select names from table4
       ) list
    LEFT JOIN table1 t1 ON (list.names = t1.names)
    LEFT JOIN table2 t2 ON (list.names = t2.names)
    LEFT JOIN table3 t3 ON (list.names = t3.names)
    LEFT JOIN table4 t4 ON (list.names = t4.names)