0

Hello I am a beginner at mysql. I have a table like this

|Item no.|Location|
-------------------
|192     |Japan   |
|882     |Korea   |
|982     |China   |
|111     |China   |
|02      |Japan   |
|03      |Korea   |
|04      |Japan   |

I want the resulting table to be like this

|Location|NumberofItems|
------------------------
|Japan   |      3      |
|Korea   |      2      |
|China   |      2      |

My code doesn't seem to work. Here is my code:

SELECT distinct Location, count(*) as NumberofItems FROM Products inner join Products on Price = Price

2 Answers 2

1

If you group by a specific column you get only the distinct values from that column. Then you can use aggregate functions like count on each group

SELECT Location, count(*) as NumberofItems 
FROM Products
GROUP BY Location
Sign up to request clarification or add additional context in comments.

Comments

0

You need GROUP BY command.

SELECT distinct location,count(*) as NumberofItems FROM products GROUP BY location;

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.