2

我正在努力解决这个问题。我想做的是这样的:

select [fields],
       ((select <criteria>) return 0 if no rows returned, return 1 if any rows returned) as SubqueryResult
where a=b

这可能吗?

4

3 回答 3

3

请试试:

select [fields],
       case when (select COUNT(*) from YourTable with criteria)>0 then 
            1 
       else 
            0 
       end 
       as SubqueryResult
where a=b
于 2013-11-12T09:18:46.617 回答
0

在 T-sql 中,您可以将 Exists 子句用于给定要求:

select [fields],
       case when exists (select <criteria> from <tablename> ) then 1 
            else 0 
            end as SubqueryResult
from <tablename>     
where a=b
于 2013-11-12T09:26:41.370 回答
0

同样在 TSQL 中,您可以使用ISNULL()and来做到这一点SELECT TOP 1

select [fields],
       ISNULL((SELECT TOP 1 1 FROM YourTable WHERE <criteria> ),0)
       as SubqueryResult
where a=b
于 2013-11-12T10:11:01.333 回答