Inside this article we will see the concept of MySQL Sum with if condition tutorial. This article contains a classified information about If Else condition in MySQL.
MySQL Sum with cases using if else conditional block is very useful when we need to calculate data of different cases in a single query. If else case block executes mysql queries into a conditional statement.
Learn More –
- How To Connect MySQL Database in Node Js Application
- How to Export MySQL Database Using Command Line in Ubuntu
- How to Import Database in MySQL Using Command Line
- Import and Export Data To MySQL Using Command Line
Let’s get started.
Consider Database Table Data
Let’s say we have a table in a database. Table contains data like this –
![](https://onlinewebtutorblog.com/wp-content/uploads/2022/01/mysql-sum-data-using-if-else-conditions.png)
Inside above image we have a payment_mode column which stores data like Cash, Paypal, Bank Transfer.
We want to find the total sum according to payment_method.
SELECT
SUM(CASE
WHEN `payment_mode` = "Cash"
THEN amount
ELSE 0 END
) AS total_cash,
SUM(CASE
WHEN `payment_mode` = "Bank Transfer"
THEN amount
ELSE 0 END
) AS total_bank_transfer,
SUM(CASE
WHEN `payment_mode` = "Paypal"
THEN amount
ELSE 0 END) AS total_paypal
FROM `finance_reports`
![](https://onlinewebtutorblog.com/wp-content/uploads/2022/01/mysql-sum-data-with-if-else-conditions.png)
We hope this article helped you to learn about MySQL Sum with If Condition Tutorial in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.