在 sql 中获取一年中几个月的每个月的总和

getting sum for each month for several months in a year in sql

我有以下table
image of database in use
我想获得以下结果

12500 年 1 月
2 月 16500
3 月 4500
4 月 6500

查询应该 return 所需月份的每个月的总数。 我知道怎么做了..

    $sql = "SELECT SUM(cost) as january FROM earnings WHERE month= 1 and year= '22" ;

获取给定月份的总和,但我找不到任何关于如何一次获取多个月份的信息。 我对此还很陌生

SELECT 
   SUM(cost) as cost,
   month 
FROM earnings 
WHERE year = :year  
GROUP BY month 

对在年份 (:year) 中找到的每月成本的所有条目求和 (GROUP BY) 每个 ROW 都有一列 costmonth.

如果您想“进一步”过滤月份,您可以应用另一个 AND 子句

AND (month >= 1 OR month <= 6) 一月到六月

有用的来源: https://www.mysqltutorial.org/mysql-group-by.aspx