'MySQL query to output multiple SUM values based on another column

I have an SQL database with hundred of thousands of payments stored as (not all columns are listed) :

ID the_date type store_location terminal paid
1 2022-05-18 Credit Card 1 2 2.00
2 2022-05-18 Debit Card 1 1 10.00
3 2022-05-18 Cash 1 1 50.00
4 2022-05-18 Credit Card 1 2 20.00
5 2022-05-18 Cash 1 2 10.00
6 2022-05-18 Debit Card 1 2 100.00

What I want to accomplish is show SUM results for each "type" without using foreach statements shuffling through every record which have proven to be very slow for large queries...

Basically the output should be like this :

Cash = 60.00
Credit Card = 22.00
Debit Card = 110.00

The current SQLi query is taking too much time when pulling monthly or quarterly statements. Here is the current query string :

$payments = dbquery_array("SELECT ticket_id, paid, type FROM payments WHERE location LIKE '".$location."' AND the_date BETWEEN '".$start_date."' AND '".$end_date."' ORDER BY id ASC");
if(is_array($payments)){
    foreach($payments as $paid){
        if($paid['type'] != "Trade-In" && $paid['type'] != "Reward Balance"){
            $total_income['All']['Total'] += $paid['paid'];
            $total_income['Type'][$paid['type']] += $paid['paid'];
            $ticket = dbquery("SELECT id, taxable FROM tickets WHERE id = '".$paid['ticket_id']."'");
            $counted_tickets[] = $ticket['id'];
            $exists = array_count_values($counted_tickets);
        }
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source