如何在php中设置查询结果?

时间:2014-02-04 13:06:44

标签: php mysql sql

我制作了一个php页面,其中我获得了7天的结果,但是当我运行查询时,它给了我过去1个月的结果我如何设置查询?这是我的查询:

  SELECT DATE_FORMAT(transactions.transaction_date,'%m-%d-%Y') as transaction_date,
         sum(amount)as Amount 
    FROM transactions 
   WHERE transaction_date BETWEEN current_date()-7 AND current_date() 
GROUP BY DATE_FORMAT(transactions.transaction_date,'%m-%d-%Y')

这是我的代码:

$strQuery="select DATE_FORMAT(transactions.transaction_date,'%m-%d-%Y') as transaction_date,sum(amount)as Amount from transactions where transaction_date BETWEEN current_date()-7 AND current_date() group by  DATE_FORMAT(transactions.transaction_date,'%m-%d-%Y')";
    $result = $GLOBALS ['mysqli']->query ($strQuery) or die ($GLOBALS ['mysqli']->error . __LINE__);
    while($rs = $result->fetch_assoc ())
    {
        $res[]=$rs;
    }

2 个答案:

答案 0 :(得分:4)

您需要稍微修改WHERE子句:

WHERE transaction_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE()

答案 1 :(得分:0)

你必须这样做..

  while($rs = $result->fetch_assoc ())
    {
        $res[]=$rs['transaction_date']; //<----- Pass the column name to the resultset
    }