相关文章推荐
笑点低的消防车  ·  mysql解决唯一索引重复导致的插入失败问题 ...·  3 周前    · 
彷徨的松球  ·  python ...·  1 年前    · 
痴情的大葱  ·  UART判断接收的数据为一帧的方法_gd ...·  2 年前    · 
追风的大象  ·  Python ...·  2 年前    · 
才高八斗的李子  ·  R语言-路径设置与工作目录修改R语言-路径设 ...·  2 年前    · 
Code  ›  MySQL中的NULL和空串比较 (r9笔记第52天)开发者社区
mysql test mysql update语句
https://cloud.tencent.com/developer/article/1065717
礼貌的土豆
2 年前
作者头像
jeanron100
0 篇文章

MySQL中的NULL和空串比较 (r9笔记第52天)

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 杨建荣的学习笔记 > MySQL中的NULL和空串比较 (r9笔记第52天)

MySQL中的NULL和空串比较 (r9笔记第52天)

作者头像
jeanron100
发布 于 2018-03-19 17:10:11
514 0
发布 于 2018-03-19 17:10:11
举报

今天接到一个MySQL工单,是执行几条SQL语句。我一看就感觉这语句比较有意思。 语句大体是这样的: update app_code_value set channel_id=null where task_id=378 and channel_id=''; update app_code_value set channel_id=null where task_id=379 and channel_id=''; 因为对Oracle熟悉一些,所以总是喜欢用Oracle的思维来看很多问题,大多数的情况下是相通的,但是还是有一些差别之处。这些就需要额外注意了。 如果用Oracle的眼光来看上面的SQL语句,那基本可以断定,这个语句就不用执行了。因为在Oracle里面null和空串还是不同的含义,但是使用起来的效果是一样的。 当然了关于NULL,在MySQL,Oracle中都是is null, is not null这样的语法,这个也是基本的规范。如果使用=null这样的情况,效果和oracle是一致的。 select count(*)from app_code_value where task_id=378 and channel_id=null; +----------+ | count(*) | +----------+ | 0 | +----------+ 1 row in set (0.00 sec) 在MySQL里面的null和空串是什么情况呢,我们来看看。 使用is null > select count(*)from app_code_value where task_id=378 and channel_id is null; +----------+ | count(*) | +----------+ | 90000 | +----------+ 1 row in set (14.46 sec) > select count(*)from app_code_value where task_id=378 and channel_id =''; +----------+ | count(*) | +----------+ | 90000 | +----------+ 1 row in set (14.46 sec) 如果看上面的结果,很容易会以为两者的效果是一致的。我也差点被这种情况误导。我们再来看一个。 > select count(*)from app_code_value where task_id=378 and (channel_id is null or channel_id =''); +----------+ | count(*) | +----------+ | 180000 | +----------+ 1 row in set (5.41 sec) 而直接忽略这个字段是否为空,查看所有匹配的数据,可以看出,也就这些数据了。 > select count(*)from app_code_value where task_id=378 ; +----------+ | count(*) | +----------+ | 180000 | +----------+ 1 row in set (5.41 sec) 从上面的测试可以看出,null和空串还是存在一定的差别。如果要形象一点来区分,我看到一个例子很不错,是拿真空和空气的关系来类比空串和null。 null和timestamp (root:localhost:Wed Jul 6 22:46:46 2016)[test]>create table test_null ( id int,date timestamp); insert into test_null values(1,nQuery OK, 0 rows affected (0.16 sec) (root:localhost:Wed Jul 6 22:46:51 2016)[test]>insert into test_null values(1,null); Query OK, 1 row affected (0.00 sec) (root:localhost:Wed Jul 6 22:46:51 2016)[test]>select *from test_null; +------+------+ | id | date | +------+------+ | 1 | NULL | +------+------+ 1 row in set (0.00 sec) 而要更具体一些来区分,可以使用length。当然我们可以顺带做一些测试。 create table test_null(id int,name varchar(30)); 我们来看看数字类型的表现。 insert into test_null(id) values(null); insert into test_null(id) values(''); >select *from test_null; +------+------+ | id | name | +------+------+ | NULL | NULL | | 0 | NULL | +------+------+ 2 rows in set (0.00 sec) 可以看到数字类型int的处理,空串会处理成0 我们来清空数据,看看字符型的表现。 truncate table test_null; 字符类型插入null和空串 insert into test_null(name) values(null); insert into test_null(name) values(''); 查看结果如下: >select *from test_null; +------+------+ | id | name | +------+------+ | NULL | NULL | | NULL | | +------+------+ 2 rows in set (0.00 sec) 空串的处理还是特别的。空串就是空串。 我们来看看使用length来比较这两个字段的结果。 >select length(id),id,length(name),name from test_null; +------------+------+--------------+------+ | length(id) | id | length(name) | name | +------------+------+--------------+------+ | NULL | NULL | NULL | NULL |

 
推荐文章
笑点低的消防车  ·  mysql解决唯一索引重复导致的插入失败问题_java mysql 因为联合唯一索引插入失败 抛错
3 周前
彷徨的松球  ·  python yaml文件数据按原有的数据顺序dump - pandaly - 博客园
1 年前
痴情的大葱  ·  UART判断接收的数据为一帧的方法_gd uart接受一帧数据-CSDN博客
2 年前
追风的大象  ·  Python 获取文件的创建时间,修改时间和访问时间 - anobscureretreat - 博客园
2 年前
才高八斗的李子  ·  R语言-路径设置与工作目录修改R语言-路径设置与工作目录修改-阿里云开发者社区
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号