Solutions to invalid is Null segment judgment and IFNULL() failure in MySql

Solutions to invalid is Null segment judgment and IFNULL() failure in MySql

MySql Null field judgment and IFNULL failure processing

ps: (If you don't want to see the process, just add a select outside your sql)

select a,b,c from table group by a,b,c //At this time, if a,b,c is judged to be null, it does not matter. select IFNULL(a,0),IFNULL(b,0),IFNULL(3,0) 
from (select a,b,c from table group by a,b,c ) //That's it

Let's take a look at this very simple table.

insert image description here

When I use SQL statements to group by PID and convert columns to rows, the results are displayed like this

insert image description here

No matter how I display IFNULL(SID1,0) or IF( SID1 is null,0,SID1), the result is still NULL -.-! I feel helpless.

My SQL is written like this. If you have not used the MAX function, you can ignore it and just look at IFNULL. It is obvious that I added IFNULL.

SELECT
			PID,
			MAX(CASE WHEN SID = 1 THEN IFNULL(PNUM ,0) END) AS SID1,
			MAX(CASE WHEN SID = 2 THEN IFNULL(PNUM ,0) END) AS SID2,
			MAX(CASE WHEN SID = 3 THEN IFNULL(PNUM ,0) END) AS SID3
		FROM
			(
				SELECT
					PID,
					SUM(PNUM) PNUM,
					SID
				FROM
					A
				GROUP BY
					PID
			) temp
		GROUP BY
			temp.PID

But I found that the result is still NULL, and then I found that IFNULL(), is null can only determine that the corresponding field exists in the table and is NULL, but there is obviously no such field in my table, so it cannot be determined!

After you select, the NULL value in the query is not recognized by mysql's is null and IFNULL()

Having said so much, how should we solve it?

It’s very simple, if you don’t have a watch, just get one! (Create a new Table?) What are you thinking about!

First, remove the IFNULL in the query. It has no effect.

Then use a select statement to query the result set as a table. This is equivalent to the field existing in the table you are querying and the value is NULL.

SELECT
	PID,IFNULL(SID1,0) SID1,IFNULL(SID2,0) SID2,IFNULL(SID3,0) SID3
FROM
	(
		SELECT
			PID,
			MAX(CASE WHEN SID = 1 THEN PNUM END) AS SID1,
			MAX(CASE WHEN SID = 2 THEN PNUM END) AS SID2,
			MAX(CASE WHEN SID = 3 THEN PNUM END) AS SID3
		FROM
			(
				SELECT
					PID,
					SUM(PNUM) PNUM,
					SID
				FROM
					A
				GROUP BY
					PID
			) temp
		GROUP BY
			temp.PID
	) temp1

See, the result is out hahahaha~

insert image description here

Problems encountered when using IFNULL() in MySql

About ifnull() Function

IFNULL(a,b) c

If a is not null, then the value of c is a

If a is null, then the value of c is b

Problems encountered in actual use

IFNULL(a,b) c

If a is not null but is ' ' an empty string, then the value of c is a empty string.

The actual desired result is that a is an empty string or null, and the value of c is b.

Solution

Use the if() function instead

if(a ='' or a is null,b,a) c

This means: if a is null or an empty string, c=b; otherwise c=a;

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Solution to MySQL IFNULL judgment problem
  • Summary of knowledge points related to null (IFNULL, COALESCE and NULLIF) in MySQL
  • Detailed explanation of IFNULL() and COALESCE() functions to replace null in MySQL
  • A brief discussion on ifnull() function similar to nvl() function in MySQL
  • Detailed explanation of IFNULL, NULLIF and ISNULL usage in MySql
  • A brief discussion on the usage of SQL Server's ISNULL function and MySQL's IFNULL function
  • Introduction to the difference between IFNULL, IF, and CASE in MySQL
  • Instructions for nested use of MySQL ifnull

<<:  Several reasons for not compressing HTML

>>:  How to build SFTP server and image server on Linux cloud server

Recommend

React+ts realizes secondary linkage effect

This article shares the specific code of React+ts...

vitrualBox+ubuntu16.04 install python3.6 latest tutorial and detailed steps

Because I need to use Ubuntu+Python 3.6 version t...

Example of how to use CSS3 to layout elements around a center point

This article introduces an example of how CSS3 ca...

Django+mysql configuration and simple operation database example code

Step 1: Download the mysql driver cmd enters the ...

Detailed explanation of Nginx forwarding socket port configuration

Common scenarios for Nginx forwarding socket port...

Tutorial on using portainer to connect to remote docker

Portainer is a lightweight docker environment man...

The concept of MTR in MySQL

MTR stands for Mini-Transaction. As the name sugg...

Implementation of Docker deployment of Django+Mysql+Redis+Gunicorn+Nginx

I. Introduction Docker technology is very popular...

Add a copy code button code to the website code block pre tag

Referring to other more professional blog systems...

Let's learn about the MySQL storage engine

Table of contents Preface 1. MySQL main storage e...

innodb_flush_method value method (example explanation)

Several typical values ​​of innodb_flush_method f...

Detailed explanation of the difference between docker-compose ports and expose

There are two ways to expose container ports in d...

Vue custom v-has instruction, steps for button permission judgment

Table of contents Application Scenario Simply put...