2013-07-17 104 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