Using Count to find the number of occurrences

后端 未结 4 917
太阳男子
太阳男子 2020-11-29 06:26

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 outp

相关标签:
4条回答
  • 2020-11-29 06:52

    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.

    0 讨论(0)
  • 2020-11-29 06:53

    SELECT ... GROUP BY

    http://dev.mysql.com/doc/refman/5.0/en/select.html

    For example:
    SELECT CarName, COUNT(CarName) AS CarCount FROM tbl GROUP BY CarName

    0 讨论(0)
  • 2020-11-29 06:54
    select car_made, count(*) as occurrences
    from cars
    group by car_made
    order by occurrences desc, car_made
    
    0 讨论(0)
  • 2020-11-29 07:04

    Do you mean this?

    select car_make, count(*) from cars
    group by car_makes
    
    0 讨论(0)
提交回复
热议问题