PHP MYSQL Blog Archive Menu by Year and Month

前端 未结 1 1946
你的背包
你的背包 2021-02-03 13:31

I\'m looking for an efficient way to collate all blog posts into a menu of the following format:

2012

  • August(6)
  • September(4)
1条回答
  •  有刺的猬
    2021-02-03 14:21

    From your question, I understand you're trying to come up with a query to group a number of elements by month and year. The following should do the trick:

    SELECT 
        YEAR(dateField) AS YEAR, 
        MONTH(dateField) AS MONTH,
        COUNT(*) AS TOTAL 
    FROM table 
    GROUP BY YEAR, MONTH
    

    Obviously, "dateField" being the name of your datetime/timestamp column and "table" being the name of your table.

    More information on the GROUP BY clause and aggregate functions (such as the COUNT(*) function used above) here.

    0 讨论(0)
提交回复
热议问题