
- 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
How to use a single MySQL query to count column values ignoring null?
For this, you can COUNT() method, which does not include NULL value. Let us first create a table −
mysql> create table DemoTable -> ( -> Name varchar(100), -> CountryName varchar(100) -> ); Query OK, 0 rows affected (0.49 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John',null); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Chris','US'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('Robert',null); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable values('Bob','UK'); Query OK, 1 row affected (0.57 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+--------+-------------+ | Name | CountryName | +--------+-------------+ | John | NULL | | Chris | US | | Robert | NULL | | Bob | UK | +--------+-------------+ 4 rows in set (0.00 sec)
Here is the query to use COUNT() and ignore NULL −
mysql> select count(Name) AS TotalName,count(CountryName) AS CountryWhichIsNotNull from DemoTable;
Output
This will produce the following output −
+-----------+-----------------------+ | TotalName | CountryWhichIsNotNull | +-----------+-----------------------+ | 4 | 2 | +-----------+-----------------------+ 1 row in set (0.00 sec)
- Related Articles
- Count NOT NULL values from separate tables in a single MySQL query
- Count zero, NULL and distinct values except zero and NULL with a single MySQL query
- How to set all values in a single column MySQL Query?
- How to use COUNT() and IF() in a single MySQL query?
- Get multiple count in a single MySQL query for specific column values
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- Ignore NULL values from separate tables in a single MySQL query and display count of NOT NULL records
- MySQL query to sort column values and ignoring quotes on one of the values
- How to count null values in MySQL?
- Count boolean field values within a single MySQL query?
- Use LIKE % to fetch multiple values in a single MySQL query
- A single MySQL query to search multiple words from different column values
- MySQL query to make a date column NULL?
- MySQL query to count number of duplicate values in a table column
- MySQL query to order by NULL values

Advertisements