Solution to the problem of null column in NOT IN filling pit in MySQL

Solution to the problem of null column in NOT IN filling pit in MySQL

Some time ago, when I was working on a small function in the company, I counted how many data there were in a certain situation and then modified the problem. It seemed very simple at the time, so I wrote the following SQL:

SELECT COUNT(*) FROM t1 where tl.c1 not IN (SELECT t2.c1 FROM t2);

The expected result is: how many data are in t1 and not in t2? The result is: 0, that is, all the data in t1 are in t2. However, it is easy to find that some data is in t1 but not in t2, so it feels very strange. This SQL seems to be fine. After some searching, it turns out that the c1 field of t2 contains a null value. Modifying it to the following two forms can get the expected result:

SELECT COUNT(*) FROM t1 LEFT JOIN t2 ON t1.c1 = t2.c1 WHERE t2.c1 IS NULL OR t2.c1 = '';

or

select COUNT(*) from t1 where t1.c1 not in (
select t2.c1 from t2 where t2.c1 is not null AND t2.c1 != ''
);

So it's all caused by null (I also added the empty string to avoid errors). The reason is that the implementation principle of not in is to compare each t1.c1 with each t2.c1 (the query results in the brackets) for inequality (!=).

foreach c1 in t2:
if t1.c1 != c1:
continue
else:
return false
return true

In SQL, any operation result of !=null is false, so if there is a null in t2, the query of not in will always return false, that is, the query result is empty.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of NULL values ​​in MySQL
  • MySQL NULL value processing example detailed explanation
  • MySQL NULL data conversion method (must read)
  • MySQL series of experience summary and analysis tutorials on NUll values

<<:  WeChat applet realizes the effect of swiping left to delete list items

>>:  Using MySQL database in docker to achieve LAN access

Recommend

Nginx tp3.2.3 404 problem solution

Recently I changed Apache to nginx. When I moved ...

MySQL multi-table join query example explanation

In actual projects, there are relationships betwe...

How to operate Linux file and folder permissions

Linux file permissions First, let's check the...

How to export mysql table structure to excel

The requirements are as follows Export the table ...

Steps for importing tens of millions of data into MySQL using .Net Core

Table of contents Preliminary preparation Impleme...

How to Customize Bash Command Prompt in Linux

Preface As we all know, bash (the B ourne-A gain ...

How to implement communication between Docker containers

Scenario: A laradock development environment (php...

Detailed explanation of webpack-dev-server core concepts and cases

webpack-dev-server core concepts Webpack's Co...

How to create a child process in nodejs

Table of contents Introduction Child Process Crea...

Analysis of problems caused by MySQL case sensitivity

MYSQL is case sensitive Seeing the words is belie...

Summary of methods to include file contents in HTML files

In the forum, netizens often ask, can I read the ...

Implementation of rewrite jump in nginx

1. New and old domain name jump Application scena...