1、作用于单列
select distinct name from A
2、作用于多列
select distinct name, id from A
select distinct xing, ming from B
返回的结果为两行,这说明distinct并非是对xing和ming两列“字符串拼接”后再去重的,而是分别作用于了xing和ming列。
3、COUNT统计
select count(distinct name) from A; --表中name去重后的数目, SQL Server支持,而Access不支持
count是不能统计多个字段的,下面的SQL在SQL Server和Access中都无法运行。
select distinct name from A
2、作用于多列
select distinct name, id from A
select distinct xing, ming from B
返回的结果为两行,这说明distinct并非是对xing和ming两列“字符串拼接”后再去重的,而是分别作用于了xing和ming列。
3、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;
4.distinct必须放在开头
select id, distinct name from A; --会提示错误,因为distinct必须放在开头
本文详细介绍了SQL中DISTINCT关键字的使用方法,包括其在单列或多列上的应用、与COUNT函数结合使用的方式以及语法规范等。通过实例展示了如何正确地进行数据去重统计。
1134

被折叠的 条评论
为什么被折叠?



