Distinct
-
作用:表示去掉重复的行,针对包含重复值的数据表
-
语法:
select distinct 列名称 From 表名称;
-
用distinct注意事项:如果指定了
select distinct
,那么Order by
子句中的项就必须出现在选择列表中,否则会出现错误。 -
举个例子:
SELECT DISTINCT Company From Orders order by Company ASC 是正确的;
SELECT DISTINCT Company From Orders order by Num ASC 是错误的; "Order by" 子句与 (Num) distinct 冲突
应改为:
SELECT DISTINCT Company,Num From Orders order by Num ASC
-
小结:
若是想在 order by 后面写属性名进行排序,则在 distinct 语句后面必须加上其对应的属性名;