with t1 as (
select 1 as b
union ALL
select 2 as b
union all
select null as b
)
select sum(b) from t1;
结果:3;
结论:sum计算会忽略NULL值;
2、count
with t1 as (
select 1 as b
union ALL
select 2 as b
union all
select null as b
)
select count(b) from t1;
结果:2;
结论:count会将null行忽略统计;
3、avg
with t1 as (
select 1 as b
union ALL
select 2 as b
union all
select null as b
)
select avg(b) from t1
结果:1.5
结论:avg操作会将null忽略计算;
4、max、min
with t1 as (
select 1 as b
union ALL
select 2 as b
union all
select null as b
)
select min(b) from t1
结果:1
结论:min会将Null忽略计算;
with t1 as (
select 1 as b
union ALL
select 2 as b
union all
select null as b
)
select max(b) from t1;
结果:2
结论:忽略NULL去计算;
5、if:
with t1 as (
select null as b
union ALL
select "a" as b
union all
select "c" as b
)
select sum(if(b != "a",1,0)) from t1;
结果:1;
结论:if判断的时候,如果字段值为NULL(非字符串),不会被转化为1进行计算,也就是说只会将非NULL数据进行判断,而NULL的数据将被忽略;