tags:

views:

87

answers:

2

I have to sum the weight of the certain products, but they are in different lines of MySQL. How to do it?

Here is an example of my database:

ID | Product_id | weight_kg | Quantity | 
1  | 201        | 6         | 3        | 
2  | 102        | 3         | 1        | 
3  | 103        | 4         | 4        | 
4  | 200        | 2         | 1        | 
5  | 201        | 6.3       | 7        | 
6  | 205        | 1         | 7        | 

For example I would like to know, how much is the weight of all the products starting with the product_id "2"(200,201,205).

+1  A: 
SELECT SUM(weight_kg * Quantity) FROM table_name WHERE Product_id LIKE '2%'
Martin
But that doesn't factor in quantities.
JNK
OK, now it does, you didn't ask for that in your question but in any case I should have speculated that you might have wanted that :)
Martin
+5  A: 

The answer for SQL Server would be:

SELECT SUM(weight_kg * Quantity) AS Weight
FROM table
WHERE Product_id LIKE '2%'
JNK
quantity is Quantity ...
Elf King