Count only null values in two different columns and display in one MySQL select statement?



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)
Updated on: 2020-06-30T13:57:51+05:30

469 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements