2013-07-17 101 views
0

假設我有下表。獲取mysql中所有值的總和

mysql> desc items; 
+---------------+------------------------+------+-----+---------+-------+ 
| Field   | Type     | Null | Key | Default | Extra | 
+---------------+------------------------+------+-----+---------+-------+ 
| item_value | int(11)    | YES |  | NULL |  | 
| item_quantity | smallint(5)   | YES |  | NULL |  | 
+---------------+------------------------+------+-----+---------+-------+ 

當前的SQL語句是簡單的用1

select sum(item_value) as total_value from items where user_id = ? 

量的項目如果某行已超過1個數量,我想考慮到這一點時,我加起來值爲所有用戶。跟蹤每行總數(item_quantity * item_value)還是可以在SQL語句中執行?

回答

4

你可以做算術的sum()聲明:

select sum(item_quantity * item_value) as total_value from items where user_id = ? 
+0

哦不我覺得自己很蠢。謝謝。 – luckytaxi