- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to count number of NULLs in a row with MySQL?
Use ISNULL() from MySQL. Let us first create a table −
mysql> create table DemoTable -> ( -> Number1 int, -> Number2 int -> ); Query OK, 0 rows affected (0.59 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10,NULL); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(NULL,NULL); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values(29,98); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(NULL,119); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement.
mysql> select *from DemoTable;
Output
This will produce the following output −
+---------+---------+ | Number1 | Number2 | +---------+---------+ | 10 | NULL | | NULL | NULL | | 29 | 98 | | NULL | 119 | +---------+---------+ 4 rows in set (0.00 sec)
Following is the query to count the number of NULLs in a row.
mysql> select Number1,Number2,isnull(Number1)+isnull(Number2) AS NumberofNULLS from DemoTable;
Output
This will produce the following output −
+---------+---------+--------------+ | Number1 | Number2 | NumberofNULLS| +---------+---------+--------------+ | 10 | NULL | 1 | | NULL | NULL | 2 | | 29 | 98 | 0 | | NULL | 119 | 1 | +---------+---------+--------------+ 4 rows in set (0.00 sec)
Advertisements