选择DISTINCT多列MySQL

时间:2013-07-25 08:08:22

标签: php 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

非常感谢任何帮助

1 个答案:

答案 0 :(得分:3)

你几乎拥有它:

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