

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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 does COALESCE order results with NULL and NON-NULL values?
- Display first non-null values with coalesce() in MySQL?
- Fetch maximum value from multiple columns with null and non-null values?
- Populate null columns in a MySQL table and set values
- Display only NOT NULL values from a column with NULL and NOT NULL records in MySQL
- Perform MySQL SELECT on fields containing null values?
- Count zero, NULL and distinct values except zero and NULL with a single MySQL query
- Looping in JavaScript to count non-null and non-empty values
- Working with NULL and IS NOT NULL in MySQL
- Set value only for NULL values in a MySQL table
- Counting the number of non-null or nonzero columns in a MySQL table?
- Check for NULL or NOT NULL values in a column in MySQL
- Filter dates from a table with DATE and NULL records in MySQL
Advertisements