oracle - 关于 nvarchar2 工作问题的 Oracle 12 等于和不等于 (=, !=, <>) 行为
问题描述
在 oracle 中,空 varchar2 和 null 被视为相同,因为 Oracle 在内部将空字符串更改为 NULL 值。请考虑以下示例:
--1- create table
CREATE TABLE persons(
person_id NUMBER GENERATED BY DEFAULT AS IDENTITY,
first_name VARCHAR2(50) ,
last_name VARCHAR2(50) ,
PRIMARY KEY(person_id)
);
--2-insert sample data
insert into persons (first_name,last_name) values('n1','l1');
insert into persons (first_name,last_name) values('n2',null);
insert into persons (first_name,last_name) values('n3','');
insert into persons (first_name) values('n4');
以下查询有结果:
select * from persons where last_name is null;
PERSON_ID FIRST_NAME LAST_NAME
2 n2
3 n3
4 n4
和
select * from persons where last_name is not null;
PERSON_ID FIRST_NAME LAST_NAME
1 n1 l1
我的问题 :
1-为什么这个查询结果是“没有选择行”</p>
select * from persons where last_name =''; -- no rows selected
select * from persons where last_name !=''; -- no rows selected
2-更重要的问题是以下查询的精彩行为:
select * from persons where last_name !='' or last_name =''; -- no rows selected
解决方案
1-为什么这个查询结果是“没有选择行”</p>
因为答案在于你的问题本身。
select * from persons where last_name =''; -- here '' is converted to NULL
-- so last_name = NULL
-- it will always fail as NULL can not match with other NULL also
select * from persons where last_name !=''; -- - here '' is converted to NULL
-- so last_name != NULL
-- it will always fail as NULL can not mismatch with other NULL also
2-更重要的问题是以下查询的精彩行为:
select * from persons where last_name !='' or last_name ='';
-- equal or not equal with NULL will always return false.
-- so the result is no rows for this query also
请注意,在任何表达式中,如果一侧是NULL
,它将始终返回 false。(IS NULL
或IS NOT NULL
需要用于检查空值)
推荐阅读
- python - 确定 KNN 将样本分类到哪个组
- javascript - 如何更改父div元素中所有子元素的字体大小
- bokeh - 如何实现数据列的客户端计算?
- django - 通过 URL 参数路由数据库
- c# - 如何在加载时将gridview居中并左右滚动以查看所有列?
- ionic-framework - Ionic4 构建 apk 服务 GET 方法不起作用 JSON 中的意外令牌 < 位置 0"
- prolog - 在条件头中使用整数挂起时如何处理
- javascript - 更好地重新加载具有更多内容的表单还是仅加载其他内容?
- powershell - 获取实际时间与之后值之间的时间差
- xml - DB2 数据库中 XML 数据类型的选择标准