Solve the problem of not finding NULL from set operation to mysql not like

Solve the problem of not finding NULL from set operation to mysql not like

An interesting discovery:

There is a table with a total of 1000 records. Now there is a query statement:

#Query statement 1
#Find the fields in the table whose id contains 'A' or 'B' or 'C' select * from table1 where id like '%A%' or id like '%B%' or id like '%C%';
#300 results were found successfully

Well, the query is normal, there are 300 records.

Then I typed the query again randomly...:

#Query statement 2
#Find the fields in the table whose id does not contain 'A', 'B', or 'C' select * from table1 where id not like '%A%' and id not like '%B%' and id not like '%C%';
#400 results were found successfully

Well, we found 400 items...what? ! Only 400? ? Shouldn’t there be 700? ! !

I was struck by lightning - what was wrong? ?

Logically, "find the fields in the table whose id contains 'A' or 'B' or 'C'" should be equivalent to "find the fields in the table whose id does not contain 'A', 'B', or 'C'"! This is determined by set operations!

"Find the fields in the table that have 'A' or 'B' or 'C' in their id" >> think of it as >> A∪B∪C

"Find the fields in the table whose id does not contain 'A', 'B', or 'C'" >> think of it as >> ∁UA∩∁UB∩∁UC

However, according to the operation rules, we know that: ∁U(A∪B∪C) = ∁UA ∩ ∁UB ∩ ∁UC

That is to say, shouldn't the results of the two query statements be complementary? ? Theoretically, the sum of the rows should be equal to 1000, so why are 300 missing now?

That's strange. What went wrong? ?

So change the query statement:

#Query statement 3
#Find the fields in the table whose id does not contain 'A' or 'B' or 'C' select * from table1 where id not in (select * from table1 where id like '%A%' or id like '%B%' or id like '%C%' );
#700 results were found successfully

Huh? ? How can we find the complementary set of query statement 1 in this way?

I compared the results of query statement 2 and query statement 3 and found that the 300 missing records were those where id was NULL!

In other words, not like '%A%' cannot find NULL records!

After understanding, we found that:

Found another detail knowledge~

Supplement: Note that when there is a null value in the not in query value of MySQL, the query result returned will always be empty

Now the database table is

When we use the following query:

select * from user
where username not in 
(
select username 
from user
where id != 2
)

At this time, the result of the subquery contains null values, so the result will always return an empty query instead of the expected row with id 2.

We can modify the query slightly:

select * from user
where username not in 
(
select username 
from user
where id != 2 and username is not null
)

At this time, by excluding the empty username column, you can get the expected result.

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 MySQL using not in to include null values
  • 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

<<:  Docker installation and deployment of Net Core implementation process analysis

>>:  Implementation steps of vue-element-admin to build a backend management system

Recommend

Native js to implement drop-down menu

Drop-down menus are also very common in real life...

JavaScript to implement voice queuing system

Table of contents introduce Key Features Effect d...

Should nullable fields in MySQL be set to NULL or NOT NULL?

People who often use MySQL may encounter the foll...

How to add website icon?

The first step is to prepare an icon making softwa...

JavaScript to achieve elastic navigation effect

This article shares the specific code for JavaScr...

Teach you how to make cool barcode effects

statement : This article teaches you how to imple...

javascript input image upload and preview, FileReader preview image

FileReader is an important API for front-end file...

Clever use of webkit-box-reflect to achieve various dynamic effects (summary)

In an article a long time ago, I talked about the...

How to solve the high concurrency problem in MySQL database

Preface We all know that startups initially use m...

Implementation of ssh non-secret communication in linux

What is ssh Administrators can log in remotely to...

Using jQuery to implement the carousel effect

This article shares the specific code for impleme...

What does input type mean and how to limit input

Common methods for limiting input 1. To cancel the...

The difference between KEY, PRIMARY KEY, UNIQUE KEY, and INDEX in MySQL

The problem raised in the title can be broken dow...

Tutorial on installing AutoFs mount service under Linux

Whether it is Samba service or NFS service, the m...