每个月运行一次SQL查询 - PHP

时间:2013-03-22 09:45:12

标签: php sql sql-server while-loop

我正在寻找一些帮助来运行每个月的查询。

$params = array(&$_POST['query']);

$SQL1 = "SELECT SUM(Income) AS OrderTotal 
FROM SalesDB
WHERE
SalesDB.[Posting Date] <= '2013-01-01' AND
SalesDB.[Posting Date] >= '2013-01-31'";

$options = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
$getProducts1 = sqlsrv_query($conn, $SQL1, $params, $options);
if ($getProducts1 === false)
die( var_dump(sqlsrv_errors()) ); 

while ($row = sqlsrv_fetch_array( $getProducts1, SQLSRV_FETCH_ASSOC)) 
{
$row['OrderTotal'];
}

通过这种方式,我获得1月份的总收入。有没有办法做一个循环,所以它在今年每个月运行一个查询?

所以我没有这种愚蠢的方式:

$params = array(&$_POST['query']);

    $SQL1 = "SELECT SUM(Income) AS OrderTotal 
    FROM SalesDB
    WHERE
    SalesDB.[Posting Date] <= '2013-01-01' AND
    SalesDB.[Posting Date] >= '2013-01-31'";

    $options = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
    $getProducts1 = sqlsrv_query($conn, $SQL1, $params, $options);
    if ($getProducts1 === false)
    die( var_dump(sqlsrv_errors()) ); 

$SQL2 = "SELECT SUM(Income) AS OrderTotal 
    FROM SalesDB
    WHERE
    SalesDB.[Posting Date] <= '2013-02-01' AND
    SalesDB.[Posting Date] >= '2013-02-28'";

    $options = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
    $getProducts2 = sqlsrv_query($conn, $SQL2, $params, $options);
    if ($getProducts2 === false)
    die( var_dump(sqlsrv_errors()) ); 

    while ($row1 = sqlsrv_fetch_array( $getProducts1, SQLSRV_FETCH_ASSOC) AND $row2 = sqlsrv_fetch_array( $getProducts2, SQLSRV_FETCH_ASSOC)) 
    {
    echo $row1['OrderTotal'];
echo $row2['OrderTotal'];

    }

3 个答案:

答案 0 :(得分:2)

您需要使用GROUP BY。此查询将按日期顺序获得2013年的所有收入总额:

$params = array(&$_POST['query']);

$SQL1 = "SELECT SUM(Income) AS OrderTotal
FROM SalesDB
WHERE
YEAR(SalesDB.[Posting Date]) = '2013'
GROUP BY MONTH(SalesDB.[Posting Date])
ORDER BY MONTH(SalesDB.[Posting Date])";

$options = array("Scrollable" => SQLSRV_CURSOR_KEYSET);
$getProducts = sqlsrv_query($conn, $SQL1, $params, $options);
if ($getProducts === false)
die( var_dump(sqlsrv_errors()) );

while ($row = sqlsrv_fetch_array( $getProducts, SQLSRV_FETCH_ASSOC))
{
    echo $row['OrderTotal'];
}

要为没有任何数据的日期显示0而不是NULL,您可以COALESCE结果:

$SQL1 = "SELECT COALESCE(0,SUM(Income)) AS OrderTotal
         FROM SalesDB
         WHERE YEAR(SalesDB.[Posting Date]) = '2013'
         GROUP BY MONTH(SalesDB.[Posting Date])
         ORDER BY MONTH(SalesDB.[Posting Date])";

答案 1 :(得分:1)

如果我理解你正确的话你想按月和年获得SUM(收入)吗?

你的领域是:SalesDB。[发布日期]?

我将如何做到这一点:

$sql = "SELECT SUM(Income) AS OrderTotal, YEAR(alesDB.[Posting Date]) AS year, MONTH(alesDB.[Posting Date]) AS month
FROM SalesDB
GROUP BY YEAR(alesDB.[Posting Date]), MONTH(alesDB.[Posting Date])";

答案 2 :(得分:0)

您可以按月对记录进行分组,但需要使用MONTH()才能获取日期的月份值。

SELECT  MONTH(SalesDB.[Posting Date]) MonthNo,
        DATENAME(month, SalesDB.[Posting Date]) MonthName,
        SUM(Income) OrderTotal
FROM    SalesDB
WHERE   SalesDB.[Posting Date] BETWEEN '2013-01-01' AND '2013-12-31'
GROUP   BY  MONTH(SalesDB.[Posting Date]), 
            DATENAME(month, SalesDB.[Posting Date])
ORDER   BY MonthNo ASC