具有一个DISTINCT字段的MySQL SELECT语句 [英] MySQL SELECT statment with one DISTINCT field
问题描述
我有一个汽车列表,每辆汽车都有一个 make
和 model
列.我想从列表中提取一个独特的品牌/型号组合列表.
I have a list of cars with each car having a make
and model
column. I would like to extract a list of unique make/model combinations from the list.
例如,可能有3辆Audi A3汽车,一辆红色,一辆蓝色等.我希望查询返回"Audi A3"是有效的品牌/型号,无论它出现多少次.
For example, there might be 3 x Audi A3 vehicles, one red, one blue etc. I would like the query to return that 'Audi A3' is a valid make/model regardless of how many times it appears.
这是我尝试的查询:
SELECT make, DISTINCT model FROM bs_cars ORDER BY make, model ASC
我试图将 DISTINCT
应用于 model
,因为 make
可能具有多种模型.
I have tried to apply DISTINCT
to model
only as make
would potentially have various models under it.
推荐答案
DISTINCT
选项适用于行级别,仅从一列中选择不同的值是没有意义的.这就是为什么它会保留在 SELECT
子句中出现的任何表达式之前的原因.
The DISTINCT
option applies at the row level, there is no point selecting distinct values only from one column. This is why it stays before any expression that appears in the SELECT
clause.
它将从输出中删除重复的行,并保留表中任何对( make
, model
)的单个实例.
It removes the duplicate rows from the output, keeping a single instance of any pair (make
, model
) you have in the table.
SELECT DISTINCT make, model
FROM bs_cars
ORDER BY make, model ASC
检查文档: http://dev.mysql.com/doc/refman/5.7/en/select.html
这篇关于具有一个DISTINCT字段的MySQL SELECT语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!