查询结果为空的返回直

sj6071 2009-06-13 01:29:55
如:


select num1 from biao1 where 条件


其中NUM1为decimal型,按条件查询不出这行,有就是数据库中没有符合条件的行,什么也查不出来,但是在这种情况下我想查询出的直为0,怎么写?

PS:select isnull(num1,0) from biao1 where 条件 不能实现
...全文
76 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinlingoo1 2009-06-15
  • 打赏
  • 举报
回复
DECLARE @T table(a int,b decimal(19,2))
insert into @T(a,b)
select 1,12 union all
select 7,null union all
select 5,22.3 union all
select 2,16 union all
select 3,null union all
select 6,null union all
select 9,87.4

select isnull(b,0) from @T where b is null
仙道彰 2009-06-14
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 happyflystone 的回复:]
select isnull((select num1 from biao1 where 条件), 0)

---
这种情况只是考虑了没有结果的情况,要是有条件满足的记录集大于1 行,就不对了,会出现如下报错:
服务器: 消息 512,级别 16,状态 1,行 16
子查询返回的值多于一个。当子查询跟随在 =、!=、 <、 <=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。


所以,

SQL code
if exists(select 1 from biao1 where 条件)
select num1 f…
[/Quote]
石头哥好强大,学习了
liping_ycit 2009-06-14
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 happyflystone 的回复:]
select isnull((select num1 from biao1 where 条件), 0)

---
这种情况只是考虑了没有结果的情况,要是有条件满足的记录集大于1 行,就不对了,会出现如下报错:
服务器: 消息 512,级别 16,状态 1,行 16
子查询返回的值多于一个。当子查询跟随在 =、!=、 <、 <=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。


所以,

SQL code
if exists(select 1 from biao1 where 条件)
select num1 f…
[/Quote]

学习了
abcdef406505181 2009-06-14
  • 打赏
  • 举报
回复
顶吧!
--小F-- 2009-06-13
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 happyflystone 的回复:]
select isnull((select num1 from biao1 where 条件), 0)

---
这种情况只是考虑了没有结果的情况,要是有条件满足的记录集大于1 行,就不对了,会出现如下报错:
服务器: 消息 512,级别 16,状态 1,行 16
子查询返回的值多于一个。当子查询跟随在 =、!=、 <、 <=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。


所以,

SQL code
if exists(select 1 from biao1 where 条件)
select num1 f…
[/Quote]

多谢石头哥指点
-狙击手- 2009-06-13
  • 打赏
  • 举报
回复
select isnull((select num1 from biao1 where 条件), 0)

---
这种情况只是考虑了没有结果的情况,要是有条件满足的记录集大于1 行,就不对了,会出现如下报错:
服务器: 消息 512,级别 16,状态 1,行 16
子查询返回的值多于一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。


所以,

if exists(select 1 from biao1 where 条件)
select num1 from biao1 where 条件
else
select 0 as num1


JonasFeng 2009-06-13
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 claro 的回复:]
XML code--方式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…
[/Quote]
太详细了。只能UP 了
claro 2009-06-13
  • 打赏
  • 举报
回复
--方式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
claro 2009-06-13
  • 打赏
  • 举报
回复
--方式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
claro 2009-06-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 sj6071 的帖子:]
如:
SQL codeselect num1 from biao1 where 条件
其中NUM1为decimal型,按条件查询不出这行,有就是数据库中没有符合条件的行,什么也查不出来,但是在这种情况下我想查询出的直为0,怎么写?

PS:select isnull(num1,0) from biao1 where 条件 不能实现
[/Quote]
/*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'
dingo_123432 2009-06-13
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 Leftie 的回复:]
select isnull(num1,0) from biao1 where num1 is NULL
[/Quote]
正解
  • 打赏
  • 举报
回复
delcare @num1 decimal
select @num1=num1 from biao1 where 条件
if @@rowcout>0
begin
select @num1
end
else
begin
select 0
end
ai_li7758521 2009-06-13
  • 打赏
  • 举报
回复
select isnull((select num1 from biao1 where 条件), 0)

楼主的select isnull(num1,0) from biao1 where是判断num1是不是null,而不是查询结果有没有。

-晴天 2009-06-13
  • 打赏
  • 举报
回复
以上都行...
SQL77 2009-06-13
  • 打赏
  • 举报
回复

SELECT CASE WHEN NOT EXISTS(select 1 from biao1 where 条件) THEN 0 ELSE NUM1 END AS NUM1 FROM BIAO1
--小F-- 2009-06-13
  • 打赏
  • 举报
回复
select isnull((select num1 from biao1 where 条件), 0)

feixianxxx 2009-06-13
  • 打赏
  • 举报
回复
select isnull((select num1 from biao1 where 条件),0)
dj3688 2009-06-13
  • 打赏
  • 举报
回复

select isnull(num1,0) as num1 from biao1 where 条件
饮水需思源 2009-06-13
  • 打赏
  • 举报
回复
select isnull(num1,0) from biao1 where num1 is NULL
ks_reny 2009-06-13
  • 打赏
  • 举报
回复

select num=isnull(select num1 from biao1 where 条件),0)


加载更多回复(1)

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧