
- 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
Find the count of EMPTY or NULL columns in a MySQL table?
Let us first create a table −
mysql> create table DemoTable781 ( Name varchar(100) ); Query OK, 0 rows affected (0.66 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable781 values(''); Query OK, 1 row affected (0.29 sec) mysql> insert into DemoTable781 values('Chris'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable781 values(''); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable781 values(null); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable781 values(null); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable781 values(''); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable781 values(null); Query OK, 1 row affected (0.28 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable781;
This will produce the following output -
+-------+ | Name | +-------+ | | | Chris | | | | NULL | | NULL | | | | NULL | +-------+ 7 rows in set (0.00 sec)
Here is the query to find the count of EMPTY or NULL columns in a MySQL table −
mysql> (select SUM(CASE when Name IS NULL THEN 1 ELSE 0 END) AS NullCountAndEmptyCount from DemoTable781) UNION ALL (select SUM(CASE when Name='' THEN 1 ELSE 0 END) AS NullCountAndEmptyCount from DemoTable781);
This will produce the following output -
+------------------------+ | NullCountAndEmptyCount | +------------------------+ | 3 | | 3 | +------------------------+ 2 rows in set (0.00 sec)
- Related Articles
- Counting the number of non-null or nonzero columns in a MySQL table?
- Count the number of columns in a MySQL table with Java
- Check whether a field is empty or null in MySQL?
- Populate null columns in a MySQL table and set values
- Set a custom value for NULL or empty values in MySQL
- Check for NULL or empty variable in a MySQL stored procedure
- Which one is better in MySQL - NULL or empty string?
- Update all the fields in a table with null or non-null values with MySQL
- Return only the non-empty and non-null values from a table and fill the empty and NULL values with the corresponding column values in MySQL?
- How to check if field is null or empty in MySQL?
- How do I check if a column is empty or null in MySQL?
- How to find the number of columns in a MySQL table?
- Which one is better to insert NULL or empty string in MySQL?
- Checking for Null or Empty in Java.
- MySQL query to replace null value with empty string in several columns while fetching data

Advertisements