php – 选择DISTINCT多列MySQL

我有一个超过30K行和多列的表.

例:

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

由于每个品牌和年份有很多颜色,我需要找出每辆车的颜色.

期望的结果:

2001 Nissan Max 5 colors
2001 GM Truck 10 colors

无需知道颜色有多少颜色.

我尝试了以下方法:

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

任何帮助将非常感激

最佳答案
你几乎拥有它:

SELECT make, model, year, COUNT(DISTINCT color) AS number FROM colors group by make, model, year LIMIT 10

转载注明原文:php – 选择DISTINCT多列MySQL - 代码日志