Reputation: 151
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
Reputation: 167192
You need to use GROUP BY
Clause.
SELECT `fruitname`, COUNT(*) as `totalCount`
FROM `tableName`
GROUP BY `fruitName`
Upvotes: 2
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