2017-05-25 98 views
0

我使用此查詢得到以下結果得到日期智者總: -我需要從表

$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  | 
----------------------------- 
+0

'... where date ='$ date'GROUP BY date「' – Mohammad

+0

or select distinct(date).... where date ='$ date' –

+0

您必須使用mysqli擴展而不是mysql,因爲這樣如果用戶輸入'$ date',你應該使用PDO或準備好的語句來避免sql注入。 – Mohammad

回答

0
where date = '$date' 
group by date; 

您需要按日期進行分組,以便在查詢中附加group by date;

+0

我試着用group by date;但它不工作。 – Imran