且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

按月份和年份在MySQL中

更新时间:2022-10-18 09:50:49

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

是你想要的。


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.

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

is what you want.