
- 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
Counting the number of non-null or nonzero columns in a MySQL table?
Use if() method for this. Let us first create a table −
mysql> create table DemoTable -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Number1 int, -> Number2 int -> ); Query OK, 0 rows affected (1.15 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable(Number1,Number2) values(10,20); Query OK, 1 row affected (0.41 sec) mysql> insert into DemoTable(Number1,Number2) values(0,32); Query OK, 1 row affected (0.38 sec) mysql> insert into DemoTable(Number1,Number2) values(40,0); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(Number1,Number2) values(40,50); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+----+---------+---------+ | Id | Number1 | Number2 | +----+---------+---------+ | 1 | 10 | 20 | | 2 | 0 | 32 | | 3 | 40 | 0 | | 4 | 40 | 50 | +----+---------+---------+ 4 rows in set (0.00 sec)
Following is the query to count the number of non-null or non-zero columns in a table −
mysql> select *, -> if(Number1 <> 0,1,0)+if(Number2 <> 0,1,0) AS TotalCount -> from DemoTable;
Output
This will produce the following output −
+----+---------+---------+------------+ | Id | Number1 | Number2 | TotalCount | +----+---------+---------+------------+ | 1 | 10 | 20 | 2 | | 2 | 0 | 32 | 1 | | 3 | 40 | 0 | 1 | | 4 | 40 | 50 | 2 | +----+---------+---------+------------+ 4 rows in set (0.00 sec)
- Related Articles
- Find the count of EMPTY or NULL columns in a MySQL table?
- Update all the fields in a table with null or non-null values with MySQL
- Get the number of columns in a MySQL table?
- Populate null columns in a MySQL table and set values
- Perform mathematical calculations in a MySQL table with NULL and NON-NULL values
- Counting presence of a NOT NULL value in MySQL
- How to find the number of columns in a MySQL table?
- Count the number of columns in a MySQL table with Java
- 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 know the exact number of table and columns in a MySQL database?
- How to select the table with the greatest number of columns in MySQL?
- Counting the total and true condition values in a MySQL table?
- Fetch maximum value from multiple columns with null and non-null values?
- How can we combine the values of two or more columns of MySQL table?
- Counting number of positive and negative votes in MySQL?

Advertisements