简体   繁体   English

显示最近六个月的结果

[英]show results of last six months

I'd like to show the results of a sql query of the last six months, and to show the month and year of the results, the code i'll show isn't complete because in fact it's much longer but i've tried to give the most important parts.. I used this function that returns the query ( the parameter $k is the interval...): 我想显示最近六个月的sql查询结果,并显示结果的月份和年份,我将显示的代码并不完整,因为实际上它要长得多,但我已经尝试过给出最重要的部分。.我使用此函数返回查询(参数$k是时间间隔...):


i have a loop that increments $k so that i can make the query for the six months: 我有一个递增$ k的循环,以便可以查询六个月:

for($i=0;$i<=5;$i++){
        $requete = query($i);
        SQL_QUERY($requete); // the function that executes the query
    }       

I can get the results of the last months untill january but not december because the year is 2012 not 2013. 我可以得到直到1月,但没有12月的最后几个月的结果,因为年份是2012年而不是2013年。


I'VE tried to make this so that the year will be current year -1 when the month is 12: 我试图这样做,以便当月份为12时,年份将是当前的-1年:

function print_date($y_m){
        foreach($y_m as $row){
             echo $row[0];
        if ($row[0] == 12){
             $_SESSION["y"]= $_SESSION["y"]+1;
             echo $_SESSION["y"];
        }
}

but it's not working, :/ does someone have an idea how to be able to do that ?? 但它不起作用,:/有人知道如何做到这一点吗?

You can get data for last six month with simple SQL query like this: 您可以使用以下简单的SQL查询获取过去六个月的数据:

SELECT SRC as SOURCE FROM mytable
WHERE TYPE = 'SMS' 
    AND date >= DATE_SUB(NOW(), INTERVAL 6 month)

Extends 扩展

If you nedd one month you may modify query: 如果您需要一个月,则可以修改查询:

SELECT SRC as SOURCE FROM mytable
WHERE TYPE = 'SMS' 
    AND YEAR(date) = DATE_SUB(NOW(), INTERVAL $k month)
    AND MONTH(date) = DATE_SUB(NOW(), INTERVAL $k month)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM