假设我有下表。
mysql> desc items;
+---------------+------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------------+------+-----+---------+-------+
| item_value | int(11) | YES | | NULL | |
| item_quantity | smallint(5) | YES | | NULL | |
+---------------+------------------------+------+-----+---------+-------+对于数量为1的项目,当前的SQL语句很简单。
select sum(item_value) as total_value from items where user_id = ?如果一行有1个以上的数量,我希望在将所有用户的值相加时将其考虑在内。跟踪每行的总金额(item_quantity * item_value)会更容易,还是可以在SQL语句中完成?
发布于 2013-07-17 09:33:48
您可以在sum()语句中执行此运算:
select sum(item_quantity * item_value) as total_value from items where user_id = ?https://stackoverflow.com/questions/17689635
复制相似问题