简体   繁体   English

在if else或case sql表达式中选择

[英]Select inside an if else or case sql expression

I would like to make a select expression inside a if else or case expression, but I can't get it to work. 我想在if else或case表达式中创建一个select表达式,但我无法使它工作。

I have tried the folowing 我试过了下面的事情

select
(case when DateTime1 is null then select * from LogEntries where PC_TID > '2014-03-26'
else select * from LogEntries where DateTime1 > '2014-03-26' 
end) as timeSearchParam
from LogEntries

and this 和这个

if DateTime1 is null
begin
select * from LogEntries where PC_TID > '2014-03-26' 
end
else
begin
select * from LogEntries where DateTime1 > '2014-03-26' 
end

None of them worked and I just can't figure out, what the problem is. 他们都没有工作,我只是想不通,问题是什么。

One of the ways is 其中一种方法是

select * from LogEntries where PC_TID > '2014-03-26'  and DateTime1 is null
union all
select * from LogEntries where DateTime1 > '2014-03-26'  and DateTime1 is not null
order by coalesce(DateTime1 , PC_TID)

or 要么

select * from LogEntries 
where 
(PC_TID > '2014-03-26'  and DateTime1 is null)
or
(DateTime1 > '2014-03-26'  and DateTime1 is not null)
order by coalesce(DateTime1 , PC_TID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM