0

I have a table that logs how many of an item people have, table is set up like

Name        Item          Amount
--------------------------------
Knob        Pistol          5
Knee        Pistol          2
Hat         Shotgun         3

I want to loop through the table and find out the total number of certain items. So if i searched for 'Pistol', it would output 7.

Do I have to loop through each record and add them to a number count, or is there a quicker way of doing it?

1
  • 1
    Did you tried SUM with GROUP BY? Commented Nov 7, 2011 at 17:52

2 Answers 2

4

This is a basic SQL question. You really should look at the SQL docs to learn more. The function you're looking for is SUM.

    SELECT SUM(amount) as total FROM table WHERE item = 'Pistol'

This would return

    total
    -------------
    7

Good luck.

Sign up to request clarification or add additional context in comments.

Comments

2

You could use the following query:

SELECT Sum(Amount) FROM table_name WHERE Item = 'Pistol'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.