Group by month by adding a MYSQL field

1

I have this query mysql that should only give me the sum of 2 fields grouped by month of all the years that are registered in the database.

It works partially (only the rows of the month add up to day 12 inclusive).

SQL Code:

SELECT fecha ,
SUM(entrante) AS credito,
SUM(saliente) AS debito
FROM libro
GROUP by YEAR(fecha),MONTH(fecha)
ORDER BY fecha

I am learning PHP and MYSQL and I do not know how to solve this dilemma.

Thanks ManucoBianco but I get this error

    
asked by Autoradio Rbb 18.01.2018 в 14:57
source

1 answer

2

Well, I think this will help me:

SELECT fecha ,
SUM(entrante) AS credito,
SUM(saliente) AS debito
FROM libro
WHERE YEAR(fecha)='2017'
GROUP by MONTH(fecha)

making a loop every year I have what I want:

result of the query:

fecha   credito     debito  
2017-01-04  62370   43502
2017-02-06  36360   17750
2017-03-06  26110   10268
2017-04-03  34730   30483
2017-05-09  68620   35582
2017-06-05  45780   34742
2017-07-05  53460   28368
2017-08-01  21220   17950
2017-09-02  35440   610
2017-10-02  29210   10276
2017-11-01  96110   47370
2017-12-02  98720   58362

I join every day.

If there is a way to do it in a single consultation for all the years that are stored please tell me how ........ to learn! ;)

    
answered by 18.01.2018 в 15:35