当前位置:网站首页>SQL | null value and SQL count() function
SQL | null value and SQL count() function
2022-07-21 23:49:00 【Navicat China】
As early as 2020 year 3 month , In relational database system NULL Value and its use One article introduces NULL Value and its special meaning in relational database . The article also described how to allow NULL And how to reference them in queries . In today's article , We will learn how to make NULL And SQL Count() Functions are combined to achieve various goals .
Calculation Null Value and non Null value
Count() There are two forms of functions :COUNT(*) Return all rows in the table , and COUNT(Expression) Ignore Null expression . therefore , If you provide a permission NULL The column name of the value , that Count() Will return with non NULL All rows of values .Count() The two different uses of are how we get a specific column NULL Value counting provides important clues . This is done by subtracting non NULL Number of fields , As shown below :
SELECT COUNT(*) - COUNT(<Column Name>)
Now we know how to calculate NULL That's ok 、 Not NULL Rows and all rows , Let's take an example . We will be on MySQL classicmodels Sample database Of customers Table to run this query . This is a Navicat Premium Table designer
addressline2 The field contains additional address details that do not belong to the street name and number . therefore , As we can see in this table data example , Not all addresses need it :
This query is used in three ways Count() Function to display all table rows 、 With data addressLine2 Number of rows and Null Number of value lines :
SELECT COUNT(*) AS All_Rows, COUNT(addressLine2) AS addressLine2_Count, COUNT(*) - COUNT(addressLine2) AS Null_addressLine2_Rows FROM customers;
This is a Navicat Premium In the query designer of SELECT sentence , And the result :
As expected ,addressLine2_Count and Null_addressLine2_Rows The result adds up to All_Rows Count .
Use in content analysis NULL
Count() Functional COUNT(Expression) Version ignores Null The fact that expressions are useful for compiling statistics about table data , Especially with SQL IF() Function in combination with other functions , This is basically a ternary operator SQL equivalent :
IF(predicate, true-value, false-value)
If the predicate is true, be IF The calculation for the true value , Or in the following query 1. If the predicate is false, Then the result is false Value or NULL, As the following statement shows . then COUNT take IF by 1( The predicate is true) Each row of is tabulated :
SELECT count(IF(country = 'Australia', 1, NULL)) as Australia_Count, count(IF(country = 'Germany', 1, NULL)) as Germany_Count, count(IF(country = 'Canada' OR country = 'USA', 1, NULL)) as North_America_Count, count(IF(country like 'F%', 1, NULL)) as F_Countries_Count, count(IF(creditLimit between 20000 and 1000000, 1, NULL)) as CreditLimit_Range_Count, count(*) as Total_Count FROM customers WHERE dob >= '1960-01-01';
This is a Navicat Queries and results in :
summary
In today's article , We learned how to make NULL And SQL Count() Functions are combined to achieve various goals . It's not just computation NULL He Fei NULL A way to value , When with others SQL function ( Such as IF() and SUM()) When used in combination , They can be used to compile various statistical information about data ! If you want to try Navicat 16, Can be in here download Navicat Of 14 Tianquan feature free trial .
Looking back
Navicat Poisoned | The truth is coming !
Navicat Become a member of the database Innovation Laboratory of the Academy of communications
Navicat Academic partnership program - Free education application
Navicat Technology think tank - Practical exercises and answers to various hot questions
Free trial introduction | Navciat 16 Database management tools
边栏推荐
- After modifying the IP address, use ifconfig on the terminal to check that the IP address is not updated
- postman接口测试和压力测试
- 棋盘覆盖问题
- What if win7 system forgets its login password? (without startup disk)
- JMeter之以页面形式保存测试过程数据
- Common functions of Charles
- Install cross compiler: eabi-4.3.3_ EmbedSky_ 20100610.tar. bz2
- 软件测试常问面试题【三】
- WebService (soap) request of JMeter
- Datagrip 2021 uses settings that make it smoother to use
猜你喜欢
Special test of APP compatibility
What should we do if the product is always in temporary demand
Audio and video development learning notes (I)
Servlet 的编程技术
Day01 software testing foundation summary
Audio and video learning notes (Thor) - Technical Analysis
Datagrip 2021 uses settings that make it smoother to use
Build server intranet Yum warehouse
使用postman批量运行接口时判断运行结果成功与否的常用断言
charles常用功能
随机推荐
Postman's solution to "failed to obtain token through data.token in tests"
MySQL学习笔记
[tips] quickly pop up the CMD window under the current directory and the path is the current path
The company has only one test, but the leader asked me to test 10 projects
Page redirection
Leetcode notes 88 Merge two ordered arrays
知秋
Follow the guidelines and improve yourself quickly: how to quickly turn functional testing to automated testing
JDBC访问数据库
JDBC access database
棋盘覆盖问题
Test case management tool recommendation
How to write the use case of APP registration function?
What should we do if the product is always in temporary demand
win7系统忘记登录密码怎么办?(不用启动盘情况下)
Practical exercise | export MySQL tables to CSV
Lamp架构——mysql集群及组复制(3)
sysstat安装并升级到11.5.5版本
Datagrip 2021 uses settings that make it smoother to use
设置表单