簡體   English   中英

如何從PHP中的數據庫中獲取上個月的數據?

[英]How to fetch previous month's data from database in php?

我正在開發一個php網站,需要從上個月獲取一些數據。 不僅是前一個月的數據,還包括最近五個月的數據。 我很困惑我該怎么做? 在數據庫中,我創建了兩列。 一個是“月”,另一個是“年”。 所以行看起來像


id ----文字----月----年

1 ----你好----五月----- 2014

2 ----嗨----五月----- 2014


現在,我可以通過使用date()函數來獲取數據。

$first_month = date('F', strtotime('-1 month'));
$second_month = date('F', strtotime('-2 month'));
$third_month = date('F', strtotime('-3 month'));

在五個月內,我可以創建五個變量。 但是一年? 我的意思是,如果當前月份是2014年1月,則我需要從2013年12月開始獲取數據,那么它將如何工作? 有人可以建議我嗎?

我感到很困惑。 請告訴我以前是否有人做過這樣的事情。 我需要想法或概念。

您可以重組表以使用Unix時間戳或MySQL date ,然后使用比較器進行選擇。 或者,您可以創建臨時表並執行相同的操作。

您要前往的方向對於維護和調試而言並非難事。 另外,正確的查詢將比我認為的要復雜。 例如,跨年將是上述的一行。 盡管許多線路都按您的方式運行。

編輯:要維持您當前的邏輯,SQL Select語句必須是一系列WHERE 例如:

SELECT * FROM table
WHERE
    (month IN ('April', 'May') AND year = 2014)

將選擇2014年的4月和5月。

以下內容將持續一年:

SELECT * FROM table
WHERE
    (month IN ('December') AND year = 2013)
OR
    (month IN ('January') AND year = 2014)

從程序設計的角度來看,您需要生成一系列的年月,包括一年中要選擇的所有月份。

這種數組的結構如下所示:

// For the first example
$where1 = array(
    2014 => array(
        'April',
        'May'
    )
);

// For the second example
$where2 = array(
    2013 => array(
        'December'
    ),
    2014 => array(
        'January'
    )
);

而且您將必須從這些數組創建WHERE子句。 如:

/**
 * Outputs
 * [where1] (month IN ('April', 'May') AND year = 2014)
 *
 * [where2] (month IN ('December') AND year = 2013) OR (month IN ('January') AND year = 2014)
 */
foreach(array('where1','where1') as $where_name) {

    $clause = '';

    $where = $$where_name;

    $wheres = array();

    foreach($where as $year => $months) {
        $wheres[] = "month IN ('" . implode("', '", $months) . "') AND year = $year";
    }

    $clause = '('.implode (') OR (', $wheres).')';

    echo "[$where_name] $clause\n\n";
}

這應該夠了吧。 有一個工作示例這里 收集月份的清單幾乎與您一直在做的一樣,但是更像這樣:

    list($month, $year) = explode(" ", date('F Y', strtotime('-3 month')));

var_dump($month); // February
var_dump($year); // 2014

$array[$year][] = $month; // Will create the array structure as above

有更有效的方法,但這是總的想法。 我相信我已經為您提供了一些有趣的工具來解決您的問題。

要使其達到一定范圍,您需要執行以下操作:

$where3 = array();

for ($prev_months = 1; $prev_months <= 5; $prev_months++) {
    list($month, $year) = explode(" ", date('F Y', strtotime("-$prev_months month")));
    $where3[$year][] = $month;
}

您可以在此處查看工作示例: http : //ideone.com/UWI5wI

供參考,以下是使用您的方法與公認准則的示例:

// your last and 5
(month IN ('April', 'March', 'February', 'January') AND year = 2014) OR (month IN ('December') AND year = 2013)

// native 1 and 5
date <= 2014-05-01 01:00:00 AND date > 2013-12-01 01:00:00

我使用了第一個(這不是約定),因為每個月都有一個。 您可能希望使用目標月份的最后一天,而不是下個月的第一天,但​​是您得到了。

跨越很長一段時間后,差異更加明顯:

// 2 years, yours
(month IN ('April', 'March', 'February', 'January') AND year = 2014) OR (month IN ('December', 'November', 'October', 'September', 'August', 'July', 'June', 'May', 'April', 'March', 'February', 'January') AND year = 2013) OR (month IN ('December', 'November', 'October', 'September', 'August', 'July', 'June', 'May') AND year = 2012)

// The conventional way is still just two comparators...

MySQL日期格式為YYYY-MM-DD HH:MM:SS

或(獲取一個月前的日期格式):

date("Y-m-d H:i:s",strtotime("-1 month"));

您應該在新數據上發布一列“創建的(時間戳/日期時間)”,並簡單地搜索這些數據。

通過創建月,日,年列並嘗試將其分開,只會使您自己更難。

如果要保留表結構,可以使用以下內容:

$month = date('F', strtotime('-6 month'));
$year = date('Y', strtotime('-6 month'));

會讓你十一月和2013年

不確定要存儲的數據類型,但另一種選擇可能是根據年份命名表,然后每個月都有列

表名稱:year_2014

 id----m1---m2---m3 etc...
 1----hello
 2----hi
 3----Hey

那么您的查詢將是:

$month = date("n", strtotime('-6 month'));
$year = date('Y', strtotime('-6 month'));
$monthval = "m" + $month;
$SQL = "SELECT " . $monthval . " FROM year_".$year;

SELECT m11 FROM year_2013

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM