表:
tab_tasks
id|....|date_completed|completed
1 | | 2016-11-05 | Y
2 | | 2016-11-07 | N
3 | | 2016-11-09 | Y
4 | | 2016-12-11 | Y
5 | | 2017-01-15 | Y
6 | | 2017-01-30 | Y
依旧......
我需要帮助编写sql查询,通过提供当前日期并将计算完成任务的月份恢复到最多12个月,例如:
month | num_complited
november | 2
december | 1
january | 2
或者如果更容易代替字母表中的字母,则月份可以是列月中的数字11,12,01。
如何实现这一目标?有没有需要任何PHP代码或只能用sql查询完成?或者我应该在该表中添加额外的列,例如月份?
答案 0 :(得分:1)
你应该放一些日期范围,例如11-2016有6个计数而11-2017有5个所以输出会显示11个计数数据混合
SELECT MONTHNAME(date_completed) as month ,
count(completed) as num_complited
from tab_tasks
where completed='Y' and
date_completed between '2016-11-05' and '2017-11-05'
group by MONTHNAME(date_completed)
答案 1 :(得分:0)
试试这个:
select MONTHNAME(date_completed) as MosName,count(1) as num_completed
from TableName
where completed = 'Y'
GROUP BY MONTHNAME(date_completed);