
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count Null Values in Two Different Columns in MySQL
Use IS NULL to test for NULL value. Let us first create a table −
mysql> create table DemoTable -> ( -> Number1 int, -> Number2 int -> ); Query OK, 0 rows affected (0.62 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(1,NULL); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(NULL,NULL); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(3,NULL); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(NULL,90); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+---------+---------+ | Number1 | Number2 | +---------+---------+ | 1 | NULL | | NULL | NULL | | 3 | NULL | | NULL | 90 | +---------+---------+ 4 rows in set (0.00 sec)
Following is the query to count only null values in two different columns and display in one select statement −
mysql> select -> (select count(*) from DemoTable where Number1 is null) as FirstColumnNullValue, -> (select count(*) from DemoTable where Number2 is null) as SecondColumnNullValue -> ;
Output
This will produce the following output −
+----------------------+-----------------------+ | FirstColumnNullValue | SecondColumnNullValue | +----------------------+-----------------------+ | 2 | 3 | +----------------------+-----------------------+ 1 row in set (0.00 sec)
Advertisements