dongzhilian0188 2019-07-15 09:18
浏览 141
已采纳

带有条件的PHP SQL SUM列

I have table table_order like below :

orderid | orderdate   | price  | deptnr
--------+-------------+--------+----------
1       | 2019-07-12  | 50000  | 1
2       | 2019-07-12  | 30000  | 1
3       | 2019-07-13  | 40000  | 1
4       | 2019-07-14  | 50000  | 1

I need to output all column on table_order with condition if there are same date, the price value will accumulate into one record (record on column 1 and 2 must be merge with total price 50000+30000). Here what i've done:

// $sql = mysqli_query($con, "SELECT * FROM table_order
//  WHERE deptnr='$departmetnnr' AND orderstatus='CLOSE'
//  AND orderdate BETWEEN '$oneyearbefore' AND '$currentdate' ORDER BY orderdate"); 

$sql = mysqli_query($con, "
    SELECT SUM(price) AS totalprice 
      FROM table_order 
     WHERE deptnr = '$departmetnnr' 
       AND orderstatus='CLOSE' 
       AND orderdate BETWEEN '$oneyearbefore' AND '$currentdate' 
     ORDER
        BY orderdate
");

$data = array();
while($row=mysqli_fetch_assoc($sql)) {
    $data[] = ($row);
}               
$json = json_encode($data);

Honestly I'm new in PHP and SQL, I try to build simple web service for Mobile Apps, so I want to process all calculation in server and output as JSON.

I have read some basic statement about PHP and SQL (in w3school, sometime in Stack Overflow answer), problem regarding with mine like this, but I not yet find the right output. I love to read any suggestion / reference link if available. Thanks in advance.

  • 写回答

2条回答 默认 最新

  • duanqiao1880 2019-07-15 09:22
    关注

    You need to GROUP BY orderdate if you want to get the sum for each date. This creates one group for each of the distinct values of orderdate, and you can then use the aggregate function SUM() to get each sum for each group (so for you, that's the sum of price for each date).

    If you do not supply a GROUP BY orderdate, you just get the sum across all rows.

    SELECT SUM(price) as totalprice, orderdate
    FROM table_order
    WHERE deptnr='$departmetnnr' 
      AND orderstatus='CLOSE' 
      AND orderdate  BETWEEN '$oneyearbefore' AND '$currentdate' 
    GROUP BY orderdate
    ORDER BY orderdate
    

    That said, you are currently injecting variables directly into the query, which should be avoided by using a prepared statement.

    $stmt = $con->prepare("SELECT SUM(price) as totalprice, orderdate
                           FROM table_order
                           WHERE deptnr=? 
                             AND orderstatus='CLOSE' 
                             AND orderdate  BETWEEN ? AND ? 
                           GROUP BY orderdate
                           ORDER BY orderdate");
    $stmt->bind_param("sss", $departmetnnr, $oneyearbefore, $currentdate);
    $stmt->execute();
    $stmt->bind_result($totalprice, $orderdate);
    while ($stmt->fetch()) {
        $data[] = ["totalprice" => $totalprice, "orderdate" => $orderdate];
    }
    $stmt->close();
    $json = json_encode($data);
    

    You can also use SQL functions to create 1 year in the past and get the current date, instead of using PHP values. Use CURDATE() to get the current date, then define an interval of 1 year which you subtract from, these will then become the ranges for your BETWEEN.

    $stmt = $con->prepare("SELECT SUM(price) as totalprice, orderdate
                           FROM table_order
                           WHERE deptnr=? 
                             AND orderstatus='CLOSE' 
                             AND orderdate  BETWEEN CURDATE() AND CURDATE() - INTERVAL 1 YEAR
                           GROUP BY orderdate
                           ORDER BY orderdate");
    $stmt->bind_param("s", $departmetnnr);
    $stmt->execute();
    $stmt->bind_result($totalprice, $orderdate);
    while ($stmt->fetch()) {
        $data[] = ["totalprice" => $totalprice, "orderdate" => $orderdate];
    }
    $stmt->close();
    $json = json_encode($data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog