SQL 数据库 学习 023 查询-06 null 的用法 --- 没有值 空值


  • 我的电脑系统:Windows 10 64位
  • SQL Server 软件版本: SQL Server 2014 Express

本篇博客里面使用了 scott 库,如何你现在还没有添加这个库到你的服务器里面,请在查看本篇博客前,访问这篇博文来在你的服务器里面附加scott库。


null — 没有值、空值

--null不能参与<> != = 运算
select * from emp where comm <> null; --输出为空 error
select * from emo where comm != null; --输出为空 error
select * from emp where comm = null; --输出为空 error
--null 可以参与is 、 not is 
select * from emp where comm is null; 
    --输出奖金为空的员工的信息
select * from emp where comm is not null;
    --输出奖金不为空的员工的信息
--任何类型的数据都允许为null
create table t1 (name nvachar(20), cnt int, riqi datetime)
insert into t1 values (null, null, null)
select * from t1;
--输出每一个员工的姓名、年薪(包含了奖金)、comm假设是一年的奖金
select ename, sal*12+comm "年薪" from emp;
    --这段指令证明了:null不能参与任何数学运算,结果永远是null
--正确的写法
select ename, sal*12+isnull(comm, 0) from emp;
    --isnull(comm, 0) 如果comm是null,就返回为0,否则返回comm的值。

总结:

  • 0null 是不一样的,null 表示空值,没有值,0表示一个确定的值
  • null 不能参与如下运算:<>!==
  • null 可以参与如下运算:isnot is
  • 任何类型的数据都允许为null
  • null不能参与任何数学运算,结果永远是null

请访问:http://www.aobosir.com/

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值