0

To make this example simple, lets pretend my table has the following attributes.

Table

ID: Int
Amount: Int
nameId: Int

Records Ex

ID: 1
Amount: 2
nameId: 3

ID: 2
Amount: 2
nameId: 3

ID: 3
Amount: 1
nameId: 3

Currently I have the following query.

SELECT DISTINCT(amount) FROM server.`inventory` where nameid = 558

It gives me

Amount 1
Amount 2

Straight forward, it selects a unique records base on amount. However, how can I also include in my query so that it counts how many id's are using that amount with nameid 558

So the output should be

Amount 1, Used 1
Amount 2, Used 2
1
  • What does your question have to do with subqueries? Commented Apr 3, 2015 at 3:01

1 Answer 1

1

This is an aggregation query. You want group by and count():

SELECT amount, COUNT(*)
FROM server.inventory 
WHERE nameid = 558
GROUP BY amount;
Sign up to request clarification or add additional context in comments.

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.