34,587
社区成员
发帖
与我相关
我的任务
分享
create table t(id int, col int)
insert t select 1, 100
--1, 正常
select * from t where id=2
/*
(0 row(s) affected)
*/
--2, 正常
select id,isnull(sum(col),0) as total from t where id=2 group by id
/*
(0 row(s) affected)
*/
--3***问题在这句:为什么会有一条记录?
select isnull(sum(col),0) as total from t where id=2
/*
total
-----------
0
(1 row(s) affected)
*/
drop table t
SELECT NULL --空值,1条记录。
SELECT NULL WHERE 1 <> 1 --空记录集,0条记录。
create table t(id int, col int)
insert t select 1, 100
select isnull(sum(col),0) as total from t where id=2
/*
total
-----------
0
(所影响的行数为 1 行)
*/
select isnull(sum(col),0) as total from t where id=2 group by id
/*total
-----------
(所影响的行数为 0 行)
*/
--1)观察如下查询和结果
select COUNT(col) as total from t where id=2
total
-----------
0
(1 row(s) affected)
select MAX(col) as total from t where id=2
total
-----------
NULL
(1 row(s) affected)
--2)相关解释
自然聚合函数MIN AVG SUM等的返回结果仍旧是NULL.
这是聚合函数的处理方式,如果结果集影响的行数为0,在聚合函数里当NULL处理