ipkiss
ipkiss

Reputation: 13651

Calculate total of some fields in mysql?

I have a table like this

item_id  item_quantity
----------------------
324      2

432      23

543      12

879      3

The item_id field is not auto-increment but it is unique.

The inputs of the query will be some item_id values and the result should be sum of those in item_quantity.

For instance, if the inputs are 324, 543, then the query should return 14 (2 + 12)

Is it possible to do this at mysql level?

Upvotes: 1

Views: 52

Answers (3)

Fabian Bigler
Fabian Bigler

Reputation: 10895

This works on MSSQL, not sure about MySQL, though:

SELECT SUM(item_quantity) FROM MyTable WHERE item_id in (324, 543)

EDIT: OK, others got the same answer. So I guess it also works on MySQL.

Upvotes: 2

Siyual
Siyual

Reputation: 16917

If all you need is the sum of the item_quantity, all you need to do is:

Select  Sum(item_quantity)
From    Table
Where   item_id In (324, 543)

Upvotes: 1

Dimitris Kalaitzis
Dimitris Kalaitzis

Reputation: 1426

Try using in :

SELECT SUM(item_quantity)
FROM table
WHERE item_id in (324,543)

Upvotes: 3

Related Questions