34,838
社区成员




select num1 from biao1 where 条件
if exists(select 1 from biao1 where 条件)
select num1 from biao1 where 条件
else
select 0 as num1
--方式1
select case when not exists(select num1 from biao1 where 条件) then 0 end
-->举例:
select case when exists (select num1 from (select 1 num1,'a' a union all select null,'b') b where a='b')
then 0 end
--方式2
select cast(isnull(num1,0) as int) from biao1 where 条件 可以实现
--举例:
select cast(isnull(num1,0) as int) num1
from (
select 1.11 num1,'a' a union all
select null,'b') b
where a='b
--方式1
select case when not exists(select num1 from biao1 where 条件) then 0 end
-->举例:
select case when exists (select num1 from (select 1 num1,'a' a union all select null,'b') b where a='b')
then 0 end
--方式2
select cast(isnull(num1,0) as int) from biao1 where 条件 可以实现
--举例:
select cast(isnull(num1,0) as int) num1
from (
select 1.11 num1,'a' a union all
select null,'b') b
where a='b
/*select num1 from biao1 where 条件
其中NUM1为decimal型,按条件查询不出这行,有就是数据库中没有符合条件的行,
什么也查不出来,但是在这种情况下我想查询出的直为0,怎么写?
PS:select isnull(num1,0) from biao1 where 条件 不能实现
*/
--方式1
select case when not exists(select num1 from biao1 where 条件) then 0 end
-->举例:
select case when exists (select num1 from (select 1 num1,'a' a union all select null,'b') b where a='b')
then 0 end
--方式2
select cast(isnull(num1,0) as int) from biao1 where 条件 可以实现
--举例:
select cast(isnull(num1,0) as int) num1
from (
select 1.11 num1,'a' a union all
select null,'b') b
where a='b'
delcare @num1 decimal
select @num1=num1 from biao1 where 条件
if @@rowcout>0
begin
select @num1
end
else
begin
select 0
end
select isnull((select num1 from biao1 where 条件), 0)
SELECT CASE WHEN NOT EXISTS(select 1 from biao1 where 条件) THEN 0 ELSE NUM1 END AS NUM1 FROM BIAO1
select isnull((select num1 from biao1 where 条件), 0)
select isnull(num1,0) as num1 from biao1 where 条件
select num=isnull(select num1 from biao1 where 条件),0)