
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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)
- Related Articles
- How to select and display a list of values in one column that are available in two different MySQL columns?
- How to select different values from same column and display them in different columns with MySQL?
- Count values based on conditions and display the result in different columns with MySQL?
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- SELECT not null column from two columns in MySQL?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Concatenate two columns when one of such column values is null in MySQL
- Select distinct values from two columns in MySQL?
- Display only the default values set for columns in MySQL
- MySQL query to display only the empty and NULL values together?
- MYSQL select DISTINCT values from two columns?
- Add values of two columns considering NULL values as zero in MySQL
- Count two different columns in a single query in MySQL?
- MySQL query to compare and display only the rows with NULL values
- Count values greater and less than a specific number and display count in separate MySQL columns?

Advertisements