Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select DISTINCT multiple columns MySQL

Tags:

php

mysql

I have a table with over 30K rows and multiple columns.

Example:

id | year | make | model | color

1  | 2001 | gm     | truck  | red
2  | 2004 | gm     | truck  | green
3  | 2001 | nissan | Max    | yellow
4  | 2001 | gm     | truck  | blue
5  | 2002 | gm     | truck  | green
6  | 2001 | nissan | Sentra | green

Since there are many color for each make model and year, I need to find out how many color for each vehicle.

Desired Results:

2001 Nissan Max 5 colors
2001 GM Truck 10 colors

No need to know what colors just how many colors.

I tried the following:

SELECT COUNT(DISTINCT make||model||year) AS number FROM colors LIMIT 10

Any help would be much appreciated

like image 486
William Orellana Avatar asked Jul 25 '13 08:07

William Orellana


1 Answers

You almost had it:

SELECT make,
       model,
       year,
       COUNT(DISTINCT color) AS number 
FROM colors
GROUP BY make, model, year
LIMIT 10;
like image 112
Orangepill Avatar answered Oct 05 '22 04:10

Orangepill