简体   繁体   English

使用 Count 查找出现次数

[英]Using Count to find the number of occurrences

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.我弄乱了UNIQUEDISTINCT ,但我无法获得左侧的值列表。

Do you mean this?你是这个意思吗?

select car_make, count(*) from cars
group by car_makes
select car_made, count(*) as occurrences
from cars
group by car_made
order by occurrences desc, car_made

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

Hope this works for you!!希望这对你有用!!

SELECT car_brand, COUNT(id) from cars
GROUP BY car_brand

COUNT(id) with InnoDB is faster than COUNT(*) because InnoDB doesn't cache the row count. COUNT(id) 与 InnoDB 比 COUNT(*) 快,因为 InnoDB 不缓存行数。

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

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