avg

avg函数.
统计a列的平均值时候.忽略a值为null的记录
insert into test(a,b) values (5,1)
insert into test(a,b) values (5,null)
insert into test(a,b) values (5,0)
insert into test(a,b) values (5,3)

select avg(t.b) from test t             // 是1.333    =4/3

select avg(nvl(t.b,0)) from test t      //是1   =4/4    不忽略列为null的记录   //nvl在 mysql是ISNULL(AGE,0)

select avg(nullif(t.b,'0')) from test t //是2   不统计为'0'的.  把0的当null来统计

select count(t.b) from test t            //是忽略b为null的记录的  .要取总记录数的话不能用count可能为null的列


not in也与null有关

 

oracle中:
NVL (expr1, expr2)->expr1为NULL,返回expr2;不为NULL,返回expr1。注意两者的类型要一致
NVL2 (expr1, expr2, expr3) ->expr1不为NULL,返回expr2;为NULL,返回expr3。expr2和expr3类型不同的话,expr3会转换为expr2的类型
NULLIF (expr1, expr2) ->相等返回NULL,不等返回expr1

原文地址:https://www.cnblogs.com/wangduqiang/p/4180853.html