Skip to content

All topics  /  MySQL

MySQL Sum with If Condition Example

MySQL ·

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...