不在具有 Null 值的列的子句中
Not in clause on a column with Null values
我有一个 table 有 10 个奇数列,其中之一是 'Status'。
我想获取状态未被拒绝的所有行,因此我在 Hive 上编写了以下查询:
select * from table1 where status <> 'Rejected'
但是 Hive 没有返回状态为 Null 的行。我将查询更改为
select * from table1 where status <> 'Rejected' or status is Null
但我找不到任何文档来理解为什么会这样。
有人可以帮我解决这个问题吗?
null
不是一个值,而是缺少值。每当您尝试在值的上下文中使用它时,结果将是 "unkonwn"。你可以这样想——"is an unknown (=null) value different from 'Rejected'? We don't know."
因此需要用is [not]
运算符专门处理。您可以将共享的第二个 where
子句视为 "all the statuses that are not known to have the value 'Rejected'".
Hive 实现了 NULL
-安全比较运算符。所以你可以这样做:
select *
from table1
where not status <=> 'Rejected' ;
至于你的问题,这是一个非常基本的问题,关于 NULL
在数据库中的含义。不是"missing",而是"unknown"。几乎所有的比较操作 return NULL
当任一操作数为 NULL
时——例外是操作数(例如 <=>
、is not null
和 is null
)专为处理 NULL
值而设计。
我有一个 table 有 10 个奇数列,其中之一是 'Status'。
我想获取状态未被拒绝的所有行,因此我在 Hive 上编写了以下查询:
select * from table1 where status <> 'Rejected'
但是 Hive 没有返回状态为 Null 的行。我将查询更改为
select * from table1 where status <> 'Rejected' or status is Null
但我找不到任何文档来理解为什么会这样。
有人可以帮我解决这个问题吗?
null
不是一个值,而是缺少值。每当您尝试在值的上下文中使用它时,结果将是 "unkonwn"。你可以这样想——"is an unknown (=null) value different from 'Rejected'? We don't know."
因此需要用is [not]
运算符专门处理。您可以将共享的第二个 where
子句视为 "all the statuses that are not known to have the value 'Rejected'".
Hive 实现了 NULL
-安全比较运算符。所以你可以这样做:
select *
from table1
where not status <=> 'Rejected' ;
至于你的问题,这是一个非常基本的问题,关于 NULL
在数据库中的含义。不是"missing",而是"unknown"。几乎所有的比较操作 return NULL
当任一操作数为 NULL
时——例外是操作数(例如 <=>
、is not null
和 is null
)专为处理 NULL
值而设计。