我需要按json格式按年份(12个月)中的月份打印Total(count)个新闻存档,如下所示:

输出:

["January:31","February:28","March:0","April:130","May:450","June:0","July:0","August:0","September:0","October:520","November:20","December:31"]


PHP代码:

SELECT COUNT(*) AS id,
YEAR(date) as `year`,
MONTH(date) as `month`,
MONTHNAME(date) as `month_name`,
FROM `aticle`
GROUP BY `year`, `month`;


注意:if we dont have news in any month print:0即:"March:0","July:0".....

我该如何打印?

最佳答案

您可以使用IFNULL来检查是否找不到数据应为0

请尝试下面给出的查询。

SELECT IFNULL(COUNT(*),0) AS id, YEAR(date) as `year`, MONTH(date) as `month`, MONTHNAME(date) as `month_name`,FROM `aticle`GROUP BY `year`, `month`;


谢谢

关于php - PHP MySql:打印总发布每月存档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24365709/

10-13 09:49