MySQL 使用 Count 查找出现次数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7654978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using Count to find the number of occurrences
提问by ATMathew
Let's say I have a table with the following values.
假设我有一个包含以下值的表。
Ford
Ford
Ford
Honda
Chevy
Honda
Honda
Chevy
So I want to construct the following output.
所以我想构建以下输出。
Ford 3
Honda 3
Chevy 2
It just takes the count of each element in the column.
它只需要列中每个元素的计数。
I'm having an issue listing the unique columns.
我在列出唯一列时遇到问题。
Can anyone tell me how to do this?
谁能告诉我如何做到这一点?
I've messed around with UNIQUE
and DISTINCT
, but I'm not able to
get the list of values on the left.
我弄乱了UNIQUE
and DISTINCT
,但我无法获得左侧的值列表。
回答by Mosty Mostacho
Do you mean this?
你是这个意思吗?
select car_made, count(*) from cars
group by car_made
回答by ajreal
select car_made, count(*) as occurrences
from cars
group by car_made
order by occurrences desc, car_made
回答by Polynomial
SELECT ... GROUP BY
选择...分组依据
http://dev.mysql.com/doc/refman/5.0/en/select.html
http://dev.mysql.com/doc/refman/5.0/en/select.html
For example:SELECT CarName, COUNT(CarName) AS CarCount FROM tbl GROUP BY CarName
例如:SELECT CarName, COUNT(CarName) AS CarCount FROM tbl GROUP BY CarName