
- 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 zero, NULL and distinct values except zero and NULL with a single MySQL query
Let us first create a table −
mysql> create table DemoTable( Value int ); Query OK, 0 rows affected (1.35 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.59 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values(20); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable values(10); Query OK, 1 row affected (0.70 sec) mysql> insert into DemoTable values(0); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(NULL); Query OK, 1 row affected (0.16 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Value | +-------+ | 10 | | NULL | | 10 | | 0 | | 20 | | 10 | | 0 | | NULL | +-------+ 8 rows in set (0.00 sec)
Following is the query to group by on all values except null and 0 −
mysql> select sum(Value is null) as NumberOfNull, sum(Value=0) as NumberOfZero, count(distinct Value > 0) as ValueExceptZeroAndNull from DemoTable;
This will produce the following output −
+--------------+--------------+------------------------+ | NumberOfNull | NumberOfZero | ValueExceptZeroAndNull | +--------------+--------------+------------------------+ | 2 | 2 | 2 | +--------------+--------------+------------------------+ 1 row in set (0.00 sec)
- Related Articles
- Ignore NULL values from separate tables in a single MySQL query and display count of NOT NULL records
- Count NOT NULL values from separate tables in a single MySQL query
- Display NULL and NOT NULL records except a single specific value in MySQL
- How to use a single MySQL query to count column values ignoring null?
- Add values of two columns considering NULL values as zero in MySQL
- Can MySQL INT type be non-zero NULL?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- MySQL query to compare and display only the rows with NULL values
- Perform mathematical calculations in a MySQL table with NULL and NON-NULL values
- Write a single MySQL query to exclude a record and display NULL value
- MySQL query to count all the column values from two columns and exclude NULL values in the total count?
- MySQL query to order by NULL values
- How to count null values in MySQL?
- MySQL query to display only the empty and NULL values together?
- MySQL query to convert empty values to NULL?

Advertisements