
- 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
MySQL Sum Query with IF Condition using Stored Procedure
The Sum() is an aggregate function in MySQL. You can use sum query with if condition. To understand the sum query with if condition, let us create a table.
The query to create a table −
mysql> create table SumWithIfCondition −> ( −> ModeOfPayment varchar(100) −> , −> Amount int −> ); Query OK, 0 rows affected (1.60 sec)
Insert some records in the table using insert command. The query is as follows −
mysql> insert into SumWithIfCondition values('Offline',10); Query OK, 1 row affected (0.21 sec) mysql> insert into SumWithIfCondition values('Online',100); Query OK, 1 row affected (0.16 sec) mysql> insert into SumWithIfCondition values('Offline',20); Query OK, 1 row affected (0.13 sec) mysql> insert into SumWithIfCondition values('Online',200); Query OK, 1 row affected (0.16 sec) mysql> insert into SumWithIfCondition values('Offline',30); Query OK, 1 row affected (0.11 sec) mysql> insert into SumWithIfCondition values('Online',300); Query OK, 1 row affected (0.17 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from SumWithIfCondition;
The following is the output −
+---------------+--------+ | ModeOfPayment | Amount | +---------------+--------+ | Offline | 10 | | Online | 100 | | Offline | 20 | | Online | 200 | | Offline | 30 | | Online | 300 | +---------------+--------+ 6 rows in set (0.00 sec)
Here is the stored procedure that takes one string as a parameter −
mysql> delimiter // mysql> create procedure sp_GetSumWithPaymentMode11(PaymentMode varchar(200)) −> begin −> select PaymentMode,sum(if(ModeOfPayment=PaymentMode,Amount,0)) as TotalAmount from SumWithIfCondition; −> end // Query OK, 0 rows affected (0.32 sec) mysql> delimiter ;
Now you can call the stored procedure using call command.
Case 1 − For Online
The query is as follows −
mysql> call sp_GetSumWithPaymentMode11('Online');
The following is the output −
+-------------+-------------+ | PaymentMode | TotalAmount | +-------------+-------------+ | Online | 600 | +-------------+-------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.01 sec)
Case 2 − For Offline
The query is as follows −
mysql> call sp_GetSumWithPaymentMode11('Offline');
The following is the output −
+-------------+-------------+ | PaymentMode | TotalAmount | +-------------+-------------+ | Offline | 60 | +-------------+-------------+ 1 row in set (0.00 sec) Query OK, 0 rows affected (0.01 sec)
- Related Articles
- MySQL Sum Query with IF Condition?
- MySQL Stored Procedure to update records with certain condition?
- Implement DELETE query in MySQL stored procedure
- Implement Conditional MySQL Query in a stored procedure?
- Implement Dynamic SQL query inside a MySQL stored procedure?
- Display records from MySQL stored Procedure with IF…THEN…END IF statements
- Implement If else in stored procedure in MySQL?
- Call Stored Procedures within a Stored Procedure with IF Logic?
- How can a MySQL stored procedure call another MySQL stored procedure inside it?
- Create a stored procedure with delimiter in MySQL
- Get database name from a query implemented in a MySQL Stored Procedure?
- MySQL stored procedure return value?
- MySQL stored-procedure: out parameter?
- MySQL Stored Procedure calling with INT and VARCHAR values
- Create variables in MySQL stored procedure with DECLARE keyword

Advertisements