简体   繁体   中英

Select DISTINCT multiple columns 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

You almost had it:

SELECT make,
       model,
       year,
       COUNT(DISTINCT color) AS number 
FROM colors
GROUP BY make, model, year
LIMIT 10;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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