简体   繁体   English

如何选择不同的列以及它们具有的记录数

[英]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|

Make use of aggregate function like COUNT() and a GROUP BY clause. 使用像COUNT()GROUP BY子句这样的聚合函数。

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

You need to use GROUP BY Clause. 您需要使用GROUP BY子句。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM