user2916886
user2916886

Reputation: 845

mysql query to count number of times each Id has been used

I have a database table named badges with following structure:

UserId   Name  

5        reviewer
3        supporter
12       copy editor
5        master
3        master

.... and so on

here name is the name of a tag achieved by the UserId.

Now I want to count for each UserId how many number of tags he has achieved and display as output in decreasing order.

Note: the UserId and Name are not distinct, means a UserId can earn multiple tags and also multiple number of each tags.

I am having writing the mysql query for this.

Upvotes: 2

Views: 3870

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726929

This is the most basic use of COUNT with GROUP BY:

SELECT UserId, COUNT(*)
FROM badges
GROUP BY UserId
ORDER BY COUNT(*) DESC

Upvotes: 1

Related Questions