Sql 仅当第一个选择为空时选择

Sql 仅当第一个选择为空时选择,sql,select,conditional,Sql,Select,Conditional,我真的不明白,用coalesce()尝试了一下,但是没有结果 我有一个选择(非常简化以理解问题): 我真的需要一个结果,所以如果这个select结果集为null(并且只有当它为null时才是)(无结果),那么下面的sql select就会出现 select col1, col2 from table1 t1 where table1.col1='another_value' and table1.col2='another_value2' 我怎样才能让

我真的不明白,用coalesce()尝试了一下,但是没有结果

我有一个选择(非常简化以理解问题):

我真的需要一个结果,所以如果这个select结果集为null(并且只有当它为null时才是)(无结果),那么下面的sql select就会出现

select col1, 
       col2 
from   table1 t1 
where  table1.col1='another_value' 
       and table1.col2='another_value2'
我怎样才能让这两个在一个大的选择?(欢迎使用任何推荐的语法…

类似于:

; WITH Base AS (

    select col1, 
           col2 
    from   table1 t1 
    where  table1.col1='value' 
           and table1.col2='value2' 
           and table1.col3='value3'
)

, Base2 AS (

    select col1, 
           col2 
    from   table1 t1 
    where  table1.col1='another_value' 
           and table1.col2='another_value2'
           AND NOT EXISTS (SELECT 1 FROM Base) -- HERE!!!

)

SELECT * FROM Base
UNION
SELECT * FROM Base2
我们希望SQL优化器不会运行第一个查询两次:-) 它是一个CTE(公共表表达式)。。。我使用它是为了可以重复使用第一个查询两次(一个在
EXISTS
中,另一个在
SELECT…UNION
中)

通过使用临时表

select col1, 
       col2 
INTO   #temp1 -- HERE!!!
from   table1 t1 
where  table1.col1='value' 
       and table1.col2='value2' 
       and table1.col3='value3'

select col1, 
       col2 
from   table1 t1 
where  table1.col1='another_value' 
       and table1.col2='another_value2'
       AND NOT EXISTS (SELECT 1 FROM #temp1) -- HERE!!!

如果您在示例中有更多的信息,我们会受益更多。两个表之间是否存在可以建立连接的公共值

SELECT  col1 
        ,col2  
FROM    Table1 t1
WHERE  table1.col1='value' 
   and table1.col2='value2' 
   and table1.col3='value3'  
UNION 
SELECT  col1 
        ,col2
FROM    Table2 t2 
WHERE  table1.col1='another_value' 
   and table1.col2='another_value2'
WHERE   NOT EXISTS (SELECT 1 FROM Table1 t1 WHERE t1.Col1 = t2.Col2)

您可以使用COALESCE,如下所示:

select COALESCE (
(select col1, 
       col2 
from   table1 t1 
where  table1.col1='value' 
       and table1.col2='value2' 
       and table1.col3='value3')
,
(select col1, 
       col2 
from   table1 t1 
where  table1.col1='another_value' 
       and table1.col2='another_value2')
)

这是我丑陋的解决方案

select top 1 with ties
       col1, 
       col2
from   table1 
where  (
          col1='value' 
          and col2='value2' 
          and col3='value3'
       ) OR
       (
          col1='another_value' 
          and col2='another_value2'
       )
order by 
          CASE
          WHEN col1='value' 
                 and col2='value2' 
                 and col3='value3'
            THEN 1
            WHEN col1='another_value' 
                 and col2='another_value2'
            THEN 2 END

可以,因为我写它只是为了表示一个问题(原始选择比这个复杂得多),谢谢,我会纠正它!你觉得我的解决方案怎么样?这么恶心,更简单的解决方案(没有with?因为我不允许使用它:S我认为在视图中也可以实现它?!@czupe是的,也是一样的。为什么不能将
一起使用?不是
SQL Server
?Oracle 10g,但它是我们的框架材料,其他开发人员在SQL-S中不使用它(100或更多类似的SQL),但我会尝试:)也许我们的框架可以理解它…再次感谢。抱歉,但不能完全理解您对“临时表”的看法,但我们可以这样说,我们将此插入临时表…但说来话长:)它正在工作(无论如何)所以接受它,因为我认为您是最快的联盟并不好,因为每次都会有第一次和第二次选择的结果集,我只想!!!第一个或第二个选择结果集(大多数情况下是第一个,如果为空,则为第二个)。但是感谢已经使用了coalesce():(但是谢谢!“我真的不明白,尝试了coalesce(),但是没有结果……”添加了WHERE子句,我想了想,不明白,为什么它会工作在t1.Col1=t2.Col2条件下?有什么意义?在我的示例中我没有得到表2:)这就是连接的作用。我假设这两个表之间有一个公共值。我说的对吗?您在示例中提供了简单的示例数据,以便我更好地理解您的表结构?
select top 1 with ties
       col1, 
       col2
from   table1 
where  (
          col1='value' 
          and col2='value2' 
          and col3='value3'
       ) OR
       (
          col1='another_value' 
          and col2='another_value2'
       )
order by 
          CASE
          WHEN col1='value' 
                 and col2='value2' 
                 and col3='value3'
            THEN 1
            WHEN col1='another_value' 
                 and col2='another_value2'
            THEN 2 END