MySQLで月と年でグループ化する 質問する

MySQLで月と年でグループ化する 質問する

Given a table with a timestamp on each row, how would you format the query to fit into this specific json object format.

I am trying to organize a json object into years / months.

json to base the query off:

{
  "2009":["August","July","September"],
  "2010":["January", "February", "October"]
}

Here is the query I have so far -

SELECT
    MONTHNAME(t.summaryDateTime) as month, YEAR(t.summaryDateTime) as year
FROM
    trading_summary t 
GROUP BY MONTH(t.summaryDateTime) DESC";

The query is breaking down because it is (predictably) lumping together the different years.

ベストアンサー1

GROUP BY YEAR(t.summaryDateTime), MONTH(t.summaryDateTime);

is what you want.

おすすめ記事