Skip to content
Advertisement

MySQL count multiple GroupBy

I have data like this

id  otherid name    
1   123     banana
2   123     banana
3   123     banana
4   456     grape
5   456     grape
6   789     orange
7   111     banana

How can I get output like this: (with MySQL query)

name    count
banana  2
grape   1
orange  1

Advertisement

Answer

Try this:

SELECT 
  f.`name`,
  COUNT(DISTINCT (f.`otherid`)) 
FROM
  `fruits` f 
GROUP BY f.`name` 
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement