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时返回指定的 值,否则返回 字段 自身的值

*/

 

转载于:https://www.cnblogs.com/MrTarget/p/5640246.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
insert table cdata_safe_income_new_day_analysis_item_status with t1 as ( select if(a.app_id is null,b.app_id,a.app_id) as app_id, if(a.product is null,b.product,a.product) as product, if(a.item_code_name is null,b.item_code_name,a.item_code_name) as item_code_name, case when a.app_id is null and (if(a.tiaozhang_cnt is null,0,a.tiaozhang_cnt) + if(b.tiaozhang_cnt is null,0,b.tiaozhang_cnt)) > 0 then '调账影响' when b.app_id is null and (if(a.tiaozhang_cnt is null,0,a.tiaozhang_cnt) + if(b.tiaozhang_cnt is null,0,b.tiaozhang_cnt)) > 0 then '调账影响' when a.app_id is null and (if(a.tuifei_cnt is null,0,a.tuifei_cnt) + if(b.tuifei_cnt is null,0,b.tuifei_cnt)) > 0 then '退费影响' when b.app_id is null and (if(a.tuifei_cnt is null,0,a.tuifei_cnt) + if(b.tuifei_cnt is null,0,b.tuifei_cnt)) > 0 then '退费影响' when a.app_id is null and c.voucher_begin > 0 then '优惠券影响' --- 原:续费使用了代金券 when a.app_id is null and c.voucher_end > 0 and change_order_cnt = 0 then '优惠券影响' --- 原:续费使用了代金券 when b.app_id is null and c.voucher_begin > 0 then '优惠券影响' --- 原:续费由代金券转为正常 when b.app_id is null and c.voucher_end > 0 and change_order_cnt = 0 then '优惠券影响' --- 原:续费由代金券转为正常 else 'item维度正常' end flag_status from (select app_id, product,item_code_name,sum(if(paymode_type = '调账',1,0)) as tiaozhang_cnt, sum(if(instr(paymode,'退费')>0,1,0)) as tuifei_cnt from cdata_safe_income_new_day where ftime = '${YYYYMMDD}' and paymode != '0元订单' and prod_class1_name = '基础安全' and prod_class2_name = '云原生安全' and product is not null and pay_type = '公有云' group by app_id,product,item_code_name ) a full join
最新发布
07-11

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值