如何使用全部合并处理多个选择语句

时间:2018-09-03 09:40:26

标签: sql sql-server

我必须通过将多个SQL语句与union all一起使用来获得结果,就像:

select column1,column2 from table1 where column4 ='value1' union all
select column1,column2 from table1 where column4 ='value2' union all
select column1,column2 from table1 where column4 ='value3' union all
select column1,column2 from table1 where column4 ='value4' union all
select column1,column2 from table1 where column4 ='value5'

对于上述情况,我只会得到两个结果:

column1  column2
XXXXX    XXXXX
XXXXX   XXXXX 

因为另外2个选择查询均无结果。但是我需要在结果表中记录空值或空值,例如:

column1  column2
XXXXX    XXXXX
Null     Null
XXXXX    XXXXX
Null     Null 

如何获得这种结果?

是否可以通过在多个sql语句中使用if语句来处理空值。

5 个答案:

答案 0 :(得分:1)

使用CTE创建值列表,然后创建UNION ALL可能会好得多,而不是使用LEFT JOIN。这使您无需扫描表5次:

WITH VTE AS (
    SELECT Column4
    FROM (VALUES('value1'),('value2'),('value3'),('value4'),('value5')) V(Column4))
SELECT YT.Column1, YT.Column2, V.Column4
FROM VTE V
     LEFT JOIN YourTable YT ON V.Column4 = YT.Column4;

答案 1 :(得分:0)

为要搜索的值创建一个表变量。然后将LEFT JOIN与另一个表一起使用。

查询

declare @t as table(
    [column4] varchar(100)
);

insert into @t
select 'value1' union all
select 'value2' union all
select 'value3' union all
select 'value4' union all
select 'value5';

select [t2].[column1], [t2].[column2]
from @t as [t1]
left join [table1] as [t2]
on [t1].[column4] = [t2].[column4];

答案 2 :(得分:0)

使用CTE:

;with vals as (
    select 'value1' as val
    union select 'value2'
    union select 'value3'
    union select 'value4'
    union select 'value5'
)
select coloumn1,coloumn2 
from vals
left join table1 on table1.coloumn4 =val

答案 3 :(得分:-1)

我将使用描述列来回答此类代码

select 'Value1' AS Value ,
        coloumn1,
        coloumn2 

from table1 

where coloumn4 ='value1' 

union all


select 'Value2' AS Value ,
        coloumn1,
        coloumn2 

from table1 

where coloumn4 ='value2' 

union all

select 'Value3' AS Value ,
        coloumn1,
        coloumn2 

from table1 

where coloumn4 ='value3' 

union all

select 'Value4' AS Value ,
        coloumn1,
        coloumn2 

from table1 

where coloumn4 ='value4' 

union all

select 'Value5' AS Value ,
        coloumn1,
        coloumn2 

from table1 

where coloumn4 ='value5' 

答案 4 :(得分:-3)

我不确定您到底想做什么,但是获得您想要的东西的最直接的方法可能是

select coloumn1,coloumn2 from table1 where coloumn4 ='value1' union all
SELECT Null, Null WHERE NOT EXISTS(select coloumn1,coloumn2 from table1 where coloumn4 ='value1') union all
select coloumn1,coloumn2 from table1 where coloumn4 ='value2' union all
SELECT Null, Null WHERE NOT EXISTS(select coloumn1,coloumn2 from table1 where coloumn4 ='value2') union all
select coloumn1,coloumn2 from table1 where coloumn4 ='value3' union all
SELECT Null, Null WHERE NOT EXISTS(select coloumn1,coloumn2 from table1 where coloumn4 ='value3') union all
select coloumn1,coloumn2 from table1 where coloumn4 ='value4' union all
SELECT Null, Null WHERE NOT EXISTS(select coloumn1,coloumn2 from table1 where coloumn4 ='value4') union all
select coloumn1,coloumn2 from table1 where coloumn4 ='value5'
SELECT Null, Null WHERE NOT EXISTS(select coloumn1,coloumn2 from table1 where coloumn4 ='value5')