Oracle 在not in中使用null的问题
总结过一下ORACLE中关于NULL的一些问题,碰巧今天在看书的过程中又看到了另外一个以前没发现的需要注意的地方,那就是在not in中
以前还专门小总结过一下Oracle中关于NULL的一些问题,碰巧今天在看书的过程中又看到了另外一个以前没发现的需要注意的地方,那就是在not in中使用null的问题。
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> select deptno
2 from dept
3 where deptno in (10,50,null);
DEPTNO
----------
10
//看到使用in的时候即便有null 也是正常的 下面看一下not in
SQL> select deptno
2 from dept
3 where deptno not in (10,50);
DEPTNO
----------
20
30
40
//这里看起来和我们的预期挺符合的哦
SQL> select deptno
2 from dept
3 where deptno not in (10,50,null);
no rows selected
//怎么回事 为什么加了个null 前面的20、30、40三条数据就不显示出来了
IN和NOT IN本质上都是OR运算,因而计算逻辑OR时处理NULL的方式不同,产生的结果也不同。
下面我们分析一下前面的三条语句
SQL> select deptno
2 from dept
3 where deptno in (10,50,null);
这里可以等价于where deptno=10 or deptno=50 or deptno=null,由于是or相连接,那么只要有一个条件为TRUE,整个就喂TRUE了。所以deptno为10的记录显示出来了。
SQL> select deptno
2 from dept
3 where deptno not in (10,50,null);
这里等价于where not (deptno=10 or deptno=50 or deptno=null),拿deptno=20的记录来举例吧。
not (20=10 or 20=50 or 20=null)
not(false or false or null)
not null
null
(以前只知道在where条件返回false的时候不成立,现在看来返回NULL的时候也不成立呀,下面是做的一个小实验可以证明这个猜想)
#####################
SQL> select * from dept
2 where 1=null;
no rows selected
#####################
SQL> select deptno
2 from dept
3 where deptno not in (10,50);
这里等价于where not (deptno=10 or deptno=50),依然拿deptno=20来举例。
not (20=10 or 20=50 )
not(false or false)
not false
true
注意:FALSE OR NULL=NULL ,,而TRUE OR NULL=TRUE。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]
