我需要从表中获得Date Wise总数

时间:2017-05-25 07:48:12

标签: php mysql

我使用此查询获得以下结果: -

$date = $row['date'];

$t_fee = mysql_query("SELECT sum(labs.test_fee), sum(labs.discount), sum(labs.received) from labs join patients on labs.p_id = patients.p_id where date = '$date'");


while($row = mysql_fetch_array($t_fee)) {


    $fee_sum = $row[0];
    $discount_sum = $row[1];
    $received = $row[2];
    $after_discount = $fee_sum - $discount_sum;
    $balance = $after_discount - $received;
-----------------------------
Date        | Total Revenue |
-----------------------------
2017-05-23  |   1,000       |
2017-05-19  |   4,190       |
2017-05-19  |   4,190       |
2017-05-19  |   4,190       |
2017-05-18  |   1,350       |
2017-05-08  |   690         |
2017-05-02  |   2,280       |
-----------------------------
Grand Total |   9,510       |
-----------------------------

我希望得到这样的结果: -

-----------------------------
Date        | Total Revenue |
-----------------------------
2017-05-23  |   1,000       |
2017-05-19  |   4,190       |
2017-05-18  |   1,350       |
2017-05-08  |   690         |
2017-05-02  |   2,280       |
-----------------------------
Grand Total |   9,510       |
-----------------------------

1 个答案:

答案 0 :(得分:0)

 where date = '$date' 
 group by date;

您需要按日期分组,因此请在查询中添加group by date;