检索特定月份的数据

时间:2014-01-02 06:21:18

标签: mysql sql date datetime select

MySQL Query就像这样

SELECT MONTHNAME(access_date) as date,
       DATE_FORMAT( access_date,  '%m/%Y' ) as month_date ,
       COUNT( log_id ) as total_count 
       FROM user_activity_log
 WHERE dam_id = (
                  SELECT dam_id
                         FROM dam_content_details 
                    WHERE content_type= '$content_type'
                 ) 
        AND access_date >= last_day(NOW() - INTERVAL ($month) MONTH)
        GROUP BY MONTH( access_date ) 
        ORDER BY access_date ASC

我将传递1,2,3等数字,然后是该月的给定值。

我遇到的问题是它每30天检索一次数据,就像那样60天。我想如果我写$ month ='1;然后它应该返回当前月份数据&上个月的数据从第1天开始。

我的示例输出 - $ month = 2

date        month_date  total_count
---------  ------------ -----------
December    12/2013     4
January     01/2014     1

我想要12月它应该从12/01/2013计算。 2013年12月1日。有任何想法如何解决?

1 个答案:

答案 0 :(得分:0)

您只需从=删除>=

试试这个:

SELECT MONTHNAME(access_date) as date,
       DATE_FORMAT( access_date,  '%m/%Y' ) as month_date ,
       COUNT( log_id ) as total_count 
FROM user_activity_log
WHERE dam_id = (SELECT dam_id FROM dam_content_details WHERE content_type= '$content_type') AND 
      access_date > LAST_DAY(NOW() - INTERVAL (($month)+1) MONTH)
GROUP BY MONTH( access_date ) 
ORDER BY access_date ASC