Reputation: 10366
Let's say I have the following table.
mysql> desc items;
+---------------+------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------------+------+-----+---------+-------+
| item_value | int(11) | YES | | NULL | |
| item_quantity | smallint(5) | YES | | NULL | |
+---------------+------------------------+------+-----+---------+-------+
The current SQL statement is simple for items with quantity of 1.
select sum(item_value) as total_value from items where user_id = ?
If a row has more than 1 quantity, I would like to take that into account when I add up the values for all users. Would it be easier to keep track of the total amount per row (item_quantity
* item_value
) or can I do it within a SQL statement?
Upvotes: 0
Views: 59
Reputation: 1269563
You can do the arithmetic in the sum()
statement:
select sum(item_quantity * item_value) as total_value from items where user_id = ?
Upvotes: 4