当前位置:网站首页>Is the SQL query result different from what you expected? Mostly "null" is making trouble

Is the SQL query result different from what you expected? Mostly "null" is making trouble

2022-06-11 01:38:00 Haoshuo programming

I wonder if my brothers feel the same

When writing query statements, we usually don't deliberately consider ”null“ value

But sometimes when comparing the query results with the database, we find that it is different from what we expected !

If you can't find the reason, it's likely to be "null" It's worth it

Now let's explore

notes : For MYSQL database

null Lead to sum The function returns null instead of 0

If sum The fields specified by the function are all... In the database null, that sum Function will return null, Instead of 0

Wrong cases

Let's first give a table data

Table name | test

Field

name varchar

mark int

Insert a piece of data into the table

Only to name assignment

mark by null

Now we are right mark Sum up

SELECT SUM(mark) FROM test

Query results :

sql The query result is not the same as expected ? Mostly "null" I'm making a mischief

Back to null, This is clearly not in line with our expectations ,

According to normal logic , We usually want to return to 0, Now we can use it ifnull Function to solve this problem

Modified SQL:

SELECT IFNULL(SUM(mark),0) FROM test

The expected query result is returned :

sql The query result is not the same as expected ? Mostly "null" I'm making a mischief

Don't forget while you study Like sharing Oh ! I will be moved to cry

sql The query result is not the same as expected ? Mostly "null" I'm making a mischief

null Lead to count Function missing Statistics

If we take some as null To count count, Then it is possible to omit the statistics

Wrong cases :

Take the above table data as an example

We use mark Field to query count value

SELECT COUNT(mark) FROM test

Query results :

There is a piece of data in our table , So the normal return result should be 1,

But because of mark by null, So it was omitted , Back to 0.

It's easy to change the plan , Just count the numbers **COUNT(*)** that will do !

Modified SQL:

SELECT COUNT(*) FROM test

The expected query result is returned :

Judge null Use "is null", Instead of "= null"

If our query criteria involve null Judge , Use "=" It's invalid

Wrong cases :

Still take the above table data as an example

Let's check mark by null The data of

SELECT * FROM  test WHERE mark = null

Query results :

sql The query result is not the same as expected ? Mostly "null" I'm making a mischief

You can see that no data is queried , This is clearly incorrect , So we should use ”is null“ This kind of writing

Modified SQL:

SELECT * FROM  test WHERE mark is null

The expected query result is returned :

sql The query result is not the same as expected ? Mostly "null" I'm making a mischief

Today we discussed null There are three cases in which the value deviates from the query result

If it's useful, brothers, remember Point like collection to support Oh !

more Programming information 、 e-book 、 Installation package On my official account of the same name “ Haoshuo programming ” China is ready for the brothers , Welcome to flirt !

原网站

版权声明
本文为[Haoshuo programming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020624519211.html