Home > Database > Mysql Tutorial > body text

Oracle SQL NULL 值对 IN/NOT IN语句的影响

WBOY
Release: 2016-06-07 15:51:01
Original
1148 people have browsed it

今天,有一个sql NOT IN语句,匹配条件里有一个null,结果什么都查不出来,同事觉得很难理解。其实只要明白一点就可以了,IN语句匹配的时候是用=,NOT IN匹配的时会用,就很容易理解了。 首先我们要知道,null在oracle是个特殊的东西,没有任何可比性,如果

  今天,有一个sql NOT IN语句,匹配条件里有一个null,结果什么都查不出来,同事觉得很难理解。其实只要明白一点就可以了,IN语句匹配的时候是用=,NOT IN匹配的时会用,就很容易理解了。

首先我们要知道,null在oracle是个特殊的东西,没有任何可比性,如果使用 =/ 对比null,得到的始终是false。null 值只能用 is null/is not null来进行对比。

已知表table:

type  value

 SC    1

 PO    4

 BO    9

 

select * from table where type IN('SC',null);

 

这句话就相当于:select * from table where type='SC' or type=null;

 

因为已经有 type='SC'匹配,而type=null没有可比性,可以认为被忽略了,所以在 IN 语句里的 null 可以认为没有意义。

 

select * from table where type NOT IN('SC',null);

 

这句话就相当于:select * from table where type!='SC' and type!=null;

 

因为 type!=null 返回的始终是false,整个查询语句不会匹配任何一条记录,所以NOT IN语句里的null会使查询没有任何结果。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!