DISTINCT 用于返回唯一不同的值
语法:
SELECT DISTINCT 列名称 FROM 表名称
DISTINCT用于多列
表A:
SQL:
select distinct name, id from A
执行后结果如下:
实际上是根据name和id两个字段来去重的,这种方式Access和SQL Server同时支持。
去重统计:COUNT统计
select count(distinct name) from A; --表中name去重后的数目, SQL Server支持,而Access不支持
count是不能统计多个字段的,下面的SQL在SQL Server和Access中都无法运行。
select count(distinct name, id) from A;
若想使用,请使用嵌套查询,如下:
select count(*) from (select distinct xing, name from B) AS M;
distinct必须放在开头
distinct语句中select显示的字段只能是distinct指定的字段,其他字段是不可能出现的。例如,假如表A有“备注”列,如果想获取distinc name,以及对应的“备注”字段,想直接通过distinct是不可能实现的。但可以通过其他方法实现.