网站首页 > 知识剖析 正文
今天使用hive查询数据时,在对字段进行条件过滤时,遇到了一个问题。
是这样的,表中有个字段数据类型为string,里面存放的值有数字,null(空值),‘’(空串),在对字段进行条件过滤时,结果并不是我期望的。
现在进行简单地总结下:
数据准备:
create table test.tb_user as
select '1' as user_id,
'aikaifa' as user_name
union all
select '2' as user_id,
'小爱' as user_name
union all
select '3' as user_id,
null as user_name
union all
select '4' as user_id,
'' as user_name
查询结果:
筛选姓名不为‘小爱’
如果我们查询sql这样写
select * from test.tb_user where user_name <>'小爱'
执行结果
细心的你,会发现查询结果只有两条记录,id为3的那条记录没有筛选出来,要知道,user_name 为null 符合条件啊,怎么就没有筛选到呢。
select * from test.tb_user where user_name <>'小爱'
通过这种方式会漏了为null的数据,需要对null进行单独的操作。
select * from test.tb_user where user_name <>'小爱' or user_name is null
筛选null
select * from test.tb_user where user_name is null
筛选空字符串
select * from test.tb_user where length(user_name)=0;
总结
<> 想要查询时,需要针对为null做特殊处理
'' 表示的是字段不为null且为空字符串,此时用 a is null 是无法查询这种值的,必须通过 a='' 或者 length(a)=0 查询
猜你喜欢
- 2024-11-21 SQL server数据运算
- 2024-11-21 SQL查询与SQL优化「姊妹篇.第四弹」
- 2024-11-21 SQL数据库常用命令
- 2024-11-21 SQL WHERE语句轻松学
- 2024-11-21 做测试不会 SQL?超详细的 SQL 查询语法教程来啦
- 2024-11-21 SQL性能优化技巧,常见优化10经验
- 2024-11-21 MyBatis常用工具类三-使用SqlRunner操作数据库
- 2024-11-21 数据库中sql语句大全
- 2024-11-21 从零开始学SQL数据分析,SQL数据提取与筛选
- 2024-11-21 一文讲懂SQL条件子句WHERE
- 04-29php开发者composer使用看这一篇就够了
- 04-29引用和变量声明在不同语言中的实作
- 04-29PHP 没你想的那么差
- 04-29Ubuntu linux 上的 Nginx 和 Php 安装
- 04-29CentOS下通过yum搭建lnmp(单版本PHP)
- 04-29为什么 PHP8 是个高性能版本
- 04-29PHP8函数包含文件-PHP8知识详解
- 04-29使用无参数函数进行命令执行
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)