Before we officially start, let's take a look at the configuration and version number information of the MySQL server, as shown in the following figure: "The food and fodder must be ready before the troops move." After reading the relevant configuration, let's create a test table and some test data. -- If the person table exists, delete it first DROP TABLE IF EXISTS person; -- Create a person table, where the username field can be empty, and set a normal index for it CREATE TABLE person ( id INT PRIMARY KEY auto_increment, name VARCHAR(20), mobile VARCHAR(13), index(name) )ENGINE='innodb'; -- Add test data to the person table insert into person(name,mobile) values('Java','13333333330'), ('MySQL','13333333331'), ('Redis','13333333332'), ('Kafka','13333333333'), ('Spring','13333333334'), ('MyBatis','13333333335'), ('RabbitMQ','13333333336'), ('Golang','13333333337'), (NULL,'13333333338'), (NULL,'13333333339'); select * from person; The constructed test data is shown in the following figure: Now that we have the data, let's look at what problems will occur when there are 1. Count data is lost When a column has a select count(*),count(name) from person; The query execution results are as follows: From the above results, we can see that when Solution If a column has a Extended knowledge: Don't use count(constant)
2. Distinct data loss When using select count(distinct name,mobile) from person; The query execution results are as follows: The original data of the database is as follows: From the above results, we can see that the 10 data in the mobile phone number column are all different, but the query result is 8. 3.Select data loss If a column has I need to query all data except those with name equal to "Java". The expected result is data with id from 2 to 10. However, when executing the following query: select * from person where name<>'Java' order by id; -- or select * from person where name!='Java' order by id; The query results are as follows: It can be seen that the two Solution To solve the above problem, you only need to add the result with select * from person where name<>'Java' or isnull(name) order by id; The final execution results are as follows: 4. Causes Null Pointer Exception If a column has a First, let's build a table and some test data: -- If the goods table exists, delete it first DROP TABLE IF EXISTS goods; -- Create the goods table CREATE TABLE goods ( id INT PRIMARY KEY auto_increment, num int )ENGINE='innodb'; -- Add test data to the goods table insert into goods(num) values(3),(6),(6),(NULL); select * from goods; The original data in the table are as follows: Next, we use the select sum(num) from goods where id>4; The query execution results are as follows:
Solving Null Pointer Exception You can avoid NullPointerException by using: select ifnull(sum(num), 0) from goods where id>4; The query execution results are as follows: 5. Increased query difficulty When a column contains The so-called increased query difficulty means that when performing a Let's take Wrong usage 1: select * from person where name<>null; The execution result is empty and no data is found, as shown in the following figure: Wrong usage 2: select * from person where name!=null; The execution result is also empty, and no data is queried, as shown in the following figure: Correct usage 1: select * from person where name is not null; The execution results are as follows: Correct usage 2: select * from person where !isnull(name); The execution results are as follows: Recommended Usage Alibaba's "Java Development Manual" recommends that we use Extended knowledge: NULL does not affect the index Careful friends may have discovered that when I created Then we use The execution result of From the above results, we can see that even if there is a Summarize In this article, we discussed five possible problems that may occur when a column is This concludes this article about 5 problems (all fatal) caused by MySQL being Null. For more information about problems caused by MySQL being Null, please search 123WORDPRESS.COM’s previous articles or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: How to use Docker plugin to remotely deploy projects to cloud servers in IDEA
>>: HTML table tag tutorial (23): row border color attribute BORDERCOLORDARK
This article example shares the specific code of ...
Table of contents Abstraction and reuse Serial Se...
Copy code The code is as follows: <body <fo...
This article records the installation and configu...
Before installing Tomcat, install the JDK environ...
Elasticsearch is very popular now, and many compa...
Use stored procedures to start transactions when ...
<br />When the page contains <img src=&qu...
Compared with other large databases such as Oracl...
This article shares the installation tutorial of ...
The downloaded version is the Zip decompression v...
Without further ado, let’s get started with resou...
Based on SEO and security considerations, a 301 r...
This article records the installation and configu...
Scenario: When page A opens page B, after operati...