MySQL Sum with If Condition Example
Hello Friends,
This article will give you example of MySQL sum query with if condition. I explained simply about MySQL sum with if multiple conditions. This tutorial will give you simple example of MySQL sum with if condition statement.
In this post, You'll learn use sum with condition in MySQL. i will show you use of sum with if condition in SQL query.
Here i will give you many example for MySQL sum with if condition.
Table: users_payment
So, let's see bellow solution:
Solution SQL Query:
SELECT
SUM(CASE
WHEN type = "CASH"
THEN amount
ELSE 0
END) AS total_cash,
SUM(CASE
WHEN type = "CREDIT_CARD"
THEN amount
ELSE 0
END) AS total_credit_card
FROM `users_payment`
Output:
It will help you...
- How to Increase max_connections in MySQL?
- How to Install the PHP PDO MySQL Extension on Ubuntu 22.04?
- How to Install PHP MySQL on Linux Ubuntu?
- How to Install LEMP Stack Nginx, MySQL, PHP on Ubuntu?
- How to Change MySQL Root User Password Ubuntu?
- PHP 8 MySQL Form Validation Example
- How to Select First Row Only in PHP MySQL?
- How To Backup MySQL Database Using PHP Example