
- 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
Perform mathematical calculations in a MySQL table with NULL and NON-NULL values
For this, you can use IFNULL() and perform mathematical calculations with NULL and NON-NULL values. Let us first create a table −
mysql> create table DemoTable1462 -> ( -> Value1 int, -> Value2 int -> ); Query OK, 0 rows affected (0.55 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1462 values(10,20); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1462 values(50,NULL); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable1462 values(NULL,70); Query OK, 1 row affected (0.25 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1462;
This will produce the following output −
+--------+--------+ | Value1 | Value2 | +--------+--------+ | 10 | 20 | | 50 | NULL | | NULL | 70 | +--------+--------+ 3 rows in set (0.00 sec)
Following is the query to perform mathematical calculations in a MySQL table with NULL and NON-NULL values −
mysql> select ifnull(Value1,0)+ifnull(Value2,0) from DemoTable1462;
This will produce the following output −
+-----------------------------------+ | ifnull(Value1,0)+ifnull(Value2,0) | +-----------------------------------+ | 30 | | 50 | | 70 | +-----------------------------------+ 3 rows in set (0.00 sec)
- Related Articles
- 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?
- Display first non-null values with coalesce() in MySQL?
- How does COALESCE order results with NULL and NON-NULL values?
- Fetch maximum value from multiple columns with null and non-null values?
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Populate null columns in a MySQL table and set values
- Perform MySQL SELECT on fields containing null values?
- Count zero, NULL and distinct values except zero and NULL with a single MySQL query
- Set value only for NULL values in a MySQL table
- Working with NULL and IS NOT NULL in MySQL
- Looping in JavaScript to count non-null and non-empty values
- Check for NULL or NOT NULL values in a column in MySQL
- Counting the number of non-null or nonzero columns in a MySQL table?
- Filter dates from a table with DATE and NULL records in MySQL

Advertisements