Group query for months

136
August 19, 2018, at 11:50 AM

I have this table.

+------------------------+--------------------+-----+
| id                     |mydate              |value|| 
+------------------------+------+-----+-------------+
| 1                      |2018-06-18 18:34:09 |50   | 
| 2                      |2018-06-18 12:34:09 |50   |
| 3                      |2018-08-18 13:34:09 |10   |
| 4                      |2018-08-18 14:34:09 |10   |
| 5                      |2018-09-18 16:34:09 |20   |
| 6                      |2018-09-18 17:34:09 |10   |
+------------------------+--------------------+-----+

I would like a Query to get something similar Group for months

I would like a consultation to achieve this

|mydate   |value|
+---------------+    
June      |100  |
August    |20   |
September |30   |

in case it is not possible with MySQL. (It could be in PHP)

Answer 1

You should use sum, group by, and MONTHNAME to get the result you need.

select sum(value), MONTHNAME(mydate) as month 
from test 
group by month

If you require the text version of the months you could swap that in PHP.

http://sqlfiddle.com/#!9/26b8fb/2

Answer 2

Here is an example:

SELECT MONTHNAME(mydate) as month, SUM(value) as value FROM myTable GROUP BY month
Rent Charter Buses Company
READ ALSO
PDO is unable to fetch database name on SQL queries

PDO is unable to fetch database name on SQL queries

I am trying to connect to the database via PDO and my dbphp file is as follows:

149
Sorting array by date in PHP

Sorting array by date in PHP

I'm tryin' to sort an array by date, but the result seems offAs you can see in the picture below the 0 index should be in the last

180
PHP - why should I use pathinfo when I can get it through $_File array

PHP - why should I use pathinfo when I can get it through $_File array

why should I use this code to get the name of the file?

125