user1625766
user1625766

Reputation: 151

How do I select distinct columns together with the count of records they have

How do I select distinct columns together with the count of records they have like if i have this data:

  banana
  apple
  orange
  banana
  apple
  apple

and I want it to display this:

  |banana|2|
  |apple |3|
  |orange|1|

Upvotes: 4

Views: 155

Answers (2)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167192

You need to use GROUP BY Clause.

SELECT `fruitname`, COUNT(*) as `totalCount`
FROM `tableName`
GROUP BY `fruitName`

Upvotes: 2

John Woo
John Woo

Reputation: 263733

Make use of aggregate function like COUNT() and a GROUP BY clause.

SELECT fruitname, COUNT(*) totalCount
FROM tableName
GROUP BY fruitName

Upvotes: 5

Related Questions