Solve the problem of MySQL using not in to include null values

Solve the problem of MySQL using not in to include null values

Notice! ! !

select * from user where uid not in (a,b,c,null);

This sql will not return any results. Avoid null in the not in list.

in addition:

– If null participates in an arithmetic operation, the value of the arithmetic expression is null. (For example: +, -, *, / addition, subtraction, multiplication and division)

– If null is involved in the comparison operation, the result can be considered false. (For example: >=,<=,<> greater than, less than, not equal to)

–If null is involved in an aggregation operation, the aggregation function will be set to null (this situation can be avoided by using methods such as isnull(field,0)). Except count(*), count(1), count(0), etc. (rows where count(field) is null are not counted).

--If there is a null value in the not in subquery, no data will be returned.

Supplement: MySQL in, not in, exists, not exists and null

Null is a strange thing in data and is also special in comparison. The following records and summarizes the impact of null on the judgment results in in, not in, exists, and not exists.

Let's make some descriptive statements. The one on the left side of the comparison operator is called the left comparison operator, and the one on the right side of the comparison operator is called the right comparison operator. For example, 1 in (1,2), then the 1 on the left side of in is the left comparison operator, and the (1,2) on the right side of in is the right comparison operator.

1.in

1.1 When the left comparison operator is null, null is returned in any case.

mysql> select null in (1,2);
+---------------+
| null in (1,2) |
+---------------+
| NULL |
+---------------+
1 row in set (0.00 sec)

mysql> select null in (1,2,null);
+--------------------+
| null in (1,2,null) |
+--------------------+
| NULL |
+--------------------+
1 row in set (0.00 sec)

1.2 When the right comparison symbol contains null, it returns 1 only when the left comparison symbol is not null and the right comparison symbol contains the left comparison symbol. In other cases, it returns null.

mysql> select null in (1,2,null);
+--------------------+
| null in (1,2,null) |
+--------------------+
| NULL |
+--------------------+
1 row in set (0.00 sec)

mysql> select 3 in (1,2,null);
+-----------------+
| 3 in (1,2,null) |
+-----------------+
| NULL |
+-----------------+
1 row in set (0.00 sec)

mysql> select 1 in (1,2,null);
+-----------------+
| 1 in (1,2,null) |
+-----------------+
| 1 |
+-----------------+
1 row in set (0.00 sec)

2. not in

2.1 When the left comparison operator is null, null is returned in any case.

mysql> select null not in (1,2,null);
+------------------------+
| null not in (1,2,null) |
+------------------------+
| NULL |
+------------------------+
1 row in set (0.00 sec)

mysql> select null not in (1,2);
+-------------------+
| null not in (1,2) |
+-------------------+
| NULL |
+-------------------+
1 row in set (0.00 sec)

2.2 When the right comparison symbol contains null, when the right comparison symbol contains the left comparison symbol, it returns 0, and null is returned in all other cases.

mysql> select 1 not in (1,2,null);
+---------------------+
| 1 not in (1,2,null) |
+---------------------+
| 0 |
+---------------------+
1 row in set (0.00 sec)

mysql> select 1 not in (2,3,null); 
+---------------------+
| 1 not in (2,3,null) |
+---------------------+
| NULL |
+---------------------+
1 row in set (0.00 sec)

3.exists

Exists is evaluated to true when the subquery returns null.

mysql> select exists (select null);
+----------------------+
| exists (select null) |
+----------------------+
| 1 |
+----------------------+
1 row in set (0.00 sec)

4. not exists

The not exists subquery is considered false if it returns null.

mysql> select not exists (select null);
+--------------------------+
| not exists (select null) |
+--------------------------+
| 0 |
+--------------------------+
1 row in set (0.00 sec)

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Why should MySQL fields use NOT NULL?
  • Solve the problem of not finding NULL from set operation to mysql not like
  • Detailed explanation of the difference between MySQL null and not null and null and empty value''''''''
  • Detailed explanation of the usage of NULL and NOT NULL when creating tables in MySQL
  • Solution to the problem of null column in NOT IN filling pit in MySQL
  • Should nullable fields in MySQL be set to NULL or NOT NULL?
  • MySQL query empty fields or non-empty fields (is null and not null)
  • mysql not in, left join, IS NULL, NOT EXISTS efficiency problem record
  • MySQL not null constraint case explanation

<<:  Using Vue3 (Part 1) Creating a Vue CLI Project

>>:  The difference and reasons between the MySQL query conditions not in and in

Recommend

Mysql tree-structured database table design

Table of contents Preface 1. Basic Data 2. Inheri...

How to install Chrome browser on CentOS 7

This article introduces how to install Chrome bro...

Use vue2+elementui for hover prompts

Vue2+elementui's hover prompts are divided in...

Ideas for creating wave effects with CSS

Previously, I introduced several ways to achieve ...

JS implements a simple todoList (notepad) effect

The notepad program is implemented using the thre...

How to create your own image using Dockerfile

1. Create an empty directory $ cd /home/xm6f/dev ...

Detailed explanation of using Vue custom tree control

This article shares with you how to use the Vue c...

Tips on HTML formatting and long files for web design

<br />Related articles: 9 practical suggesti...

Best way to replace the key in json object

JSON (JavaScript Object Notation, JS Object Notat...

MySQL 8.0.15 installation and configuration graphic tutorial

This article records the installation and configu...

JavaScript to implement slider verification code

This article shares the specific code of JavaScri...

Detailed explanation of grep and egrep commands in Linux

rep / egrep Syntax: grep [-cinvABC] 'word'...

How to dynamically modify the replication filter in mysql

MySQL dynamically modify replication filters Let ...

Docker image export, import and copy example analysis

The first solution is to push the image to a publ...

Solutions for high traffic websites

First: First, confirm whether the server hardware ...