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.