当前位置:网站首页>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 :

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 :

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

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 :

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 :

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 !
边栏推荐
- 焱融看|混合云环境下,如何实现数据湖最优存储解决方案
- Bubble sort and quick sort
- 2.2、ROS+PX4仿真多点巡航飞行----正方形
- 1.7、PX4遥控器校准
- 北京昌平区高新技术企业培育支持标准,补贴10万
- Tencent cloud database tdsql- a big guy talks about the past, present and future of basic software
- Millions of visits - resolution of high concurrency problems
- 北京中国专利奖政策支持介绍,补贴100万
- Solution to prompt "network initialization failed operation failed" in PD virtual machine installation system
- What are programmers in big factories looking at? It took me two years to sort it out, and I will look at it and cherish it!
猜你喜欢

Yunna PDA wireless fixed assets inventory management system

Record the packaging of the googlechrome browser plug-in

2.2、ROS+PX4仿真多点巡航飞行----正方形

Yunna Qingyuan fixed assets management and barcode inventory system
![[ROS] review of 2021 ROS Summer School](/img/1c/588d29b60071628c7c9fdce17e8b84.jpg)
[ROS] review of 2021 ROS Summer School
![[Li mu] how to read papers [intensive reading of papers]](/img/41/7e1ff1db2f7a848c8702c186c79fe5.jpg)
[Li mu] how to read papers [intensive reading of papers]

如何使用自定义注解进行参数校验

Direct insert sort and shell sort

Classic questions: 01 backpack, complete backpack, multiple backpack, two-dimensional cost Backpack

What are programmers in big factories looking at? It took me two years to sort it out, and I will look at it and cherish it!
随机推荐
Set up a flag -- Reconstruct promise
Configurable custom implementation 1 Implementation interface, 2 Custom configuration 3 Default configuration
About mobx
Yunna Qingyuan fixed assets management and barcode inventory system
如何使用自定义注解进行参数校验
CSRF attack
[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields
1.6、 PX4初始化校准
Beijing Dongcheng District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
云呐|PDA无线固定资产盘点管理系统
What is the C-end and what is the b-end? Let me tell you
Introduction to the policy support of Beijing China Patent Award, with a subsidy of 1million yuan
腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来
IRS application release 15: application security self test guide
How to write this with data and proc without SQL
2022 recognition requirements for new technologies and new products (services) in Huairou District, Beijing
使用 CompletableFuture
Hao expresses his opinions: what small good habits have you adhered to?
2.1 ros+px4 simulation - Fixed Point flight control
node和express实现mySql模糊搜索