7.where_如何处理NULL_isnull()

USE SQLSERVER;
SELECT * FROM EMP;

--is null如何分离数据库中的NULL空值
--is null(字段,值)

--查询comm为NULL的详细数据
    select * from emp where comm = null;--错误
    select * from emp where comm is null;

--查询奖金(comm)不为NULL的详细数据
    select * from emp where comm <> null;--错误
    select * from emp where comm != null;--错误
    select * from emp where comm is not null;
    
/*
    注意:
        NULL:        是空的,不存在的意思
        运算符号:    NULL无法作为条件或者数据进行运算

    定义:
        数据库中: NULL     和 VALUE 的概念
                   不存在        值

    语法:
        select * from * where * is (not) null;
        
    语意:
        select * from emp where comm is (not) null;
        来自emp 那个 comm 等于空 的查询, 并显示
                
        是可以判断是否有值或不存在值的方法
*/

--计算奖金(comm)*12的结果
    select 12*comm as comm from emp;--查询结果NULL数据的始终为NULL

--计算一个月工资和奖金之和
    select sal+comm from emp;--comm字段NULL并不能与sal正常的相加(不代表0)
    
    --解决办法(    聚合函数isnull(字段, 数值),当检查的字段为NULL则返回数值,否则返回原本的数值    )
    select sal + isnull(comm, 0) as "月薪+奖金" from emp;
    
--计算一年工资和奖金之和
    select 12*sal + isnull(comm, 0) as "年薪" from emp;

--计算一年工资和奖金之和(不包括comm为空NULL的)
    select distinct 12*sal+comm as "年薪" from emp where comm is not null;

/*
    注意:
        NULL 不能进行运算

    语法:
        isnull(字段,值)--当 字段 只是的属性为NULL时返回指定的 值,否则返回 字段 自身的值

*/

 

posted @ 2016-07-04 13:54  Mr_Target  阅读(733)  评论(0编辑  收藏  举报