
- 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 SUM and SUBTRACTION on the basis of a condition in a single MySQL query?
For this, use CASE statement and set for both SUM and SUBTRACTION. Let us first create a table −
mysql> create table DemoTable866( Status varchar(100), Amount int ); Query OK, 0 rows affected (0.63 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable866 values('ACTIVE',50); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable866 values('INACTIVE',70); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable866 values('INACTIVE',20); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable866 values('ACTIVE',100); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable866 values('ACTIVE',200); Query OK, 1 row affected (0.11 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable866;
This will produce the following output −
+----------+--------+ | Status | Amount | +----------+--------+ | ACTIVE | 50 | | INACTIVE | 70 | | INACTIVE | 20 | | ACTIVE | 100 | | ACTIVE | 200 | +----------+--------+ 5 rows in set (0.00 sec)
Following is the query to perform SUM or SUBTRACTION in the same column with a single MySQL query −
mysql> select sum(CASE WHEN Status = 'ACTIVE' then Amount WHEN Status = 'INACTIVE' then -Amount END) AS RemainingAmount from DemoTable866;
This will produce the following output −
+-----------------+ | RemainingAmount | +-----------------+ | 260 | +-----------------+ 1 row in set (0.06 sec)
- Related Articles
- Update only a single column in a MySQL table and increment on the basis of a condition
- MySQL query to fetch only a single field on the basis of boolean value in another field
- MySQL Sum Query with IF Condition?
- SUM a column based on a condition in MySQL
- MySQL query to group concat and place data into a single row on the basis of 1 values in corresponding column?
- Display the student marks in a single column on the basis of subject in MySQL?
- Perform group and distinct together in a single MongoDB query
- Add a column count in a MySQL query on the basis of last name records?
- Perform MySQL UPDATE on the basis of DATE value in another column
- A single query to get the sum of count from different tables in MySQL?
- Can you check for both a blank string and 0 in one condition with a single MySQL query?
- MySQL query to update different fields based on a condition?
- MySQL Sum Query with IF Condition using Stored Procedure
- MySQL query to select records from a table on the basis of a particular month number?
- A single MySQL select query on two tables is possible?

Advertisements