39

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.

dc95
  • 1,319
  • 1
  • 22
  • 44
ATMathew
  • 12,566
  • 26
  • 69
  • 76
  • 1
    I'll give you a hint: group by is what will group like rows by a column. select count(*) from table group by car_type should get you there. – anq Oct 04 '11 at 23:00

4 Answers4

63

Do you mean this?

select car_make, count(*) from cars
group by car_makes
Mosty Mostacho
  • 42,742
  • 16
  • 96
  • 123
5
select car_made, count(*) as occurrences
from cars
group by car_made
order by occurrences desc, car_made
ajreal
  • 46,720
  • 11
  • 89
  • 119
2

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

Polynomial
  • 27,674
  • 12
  • 80
  • 107
0

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.

Ankit Jindal
  • 3,672
  • 3
  • 25
  • 37