2013-07-31 74 views
0

我有下面的表,想知道如何設置一列total來計算行的總和。我想要total列計算first_bid - second_bid列計算器

id |  First Bid | Second Bid |  Total 
0 |  7  |   1  | (first_bid)-(second_bid) 
1 |  8  |   2  | 
2 |  5  |   3  | 
3 |  4  |   4  | 
4 |  5  |   5  | 
5 |  5  |   6  | 

我需要向用戶顯示之前的所有出價和總頁數。總數也應該從大到小。

+0

做些什麼?出價改變時運行它?數據顯示時運行它?超級基本示例'SELECT ...(first_bid-second_bid)作爲總數...' – 2013-07-31 20:47:31

回答

1

你可以只計算總時要查詢的數據庫。

SELECT first_bid, second_bid, (first_bid - second_bid) as total FROM table ORDER BY 3 DESC 

沿着這些線路要填充它一次性

3
SELECT id, First_Bid, Second_Bid, First_Bid - Second_Bid AS Total 

對於總計,你必須與SUM()聚合函數運行第二個查詢。這有點多餘,因爲您可以在檢索數據時在客戶端執行求和。例如

$total = 0; 
while($row = fetch_from_db($result)) { 
    $total += $result['Total']; 
    ... display row data ... 
} 
... display total ... 
0

嘗試此查詢:

SELECT *, (First Bid-Second Bid) as total from TABLE