SQL中distinct的用法
在表中,可能会包含重复值。这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值。关键词 distinct用于返回唯一不同的值。
表A:

示例1
select distinct name from A
执行后结果如下:

示例2
select distinct name, id from A
执行后结果如下:

实际上是根据“name+id”来去重
示例3:统计
select count(distinct name) from A; --表中name去重后的数目, SQL Server支持,而Access不支持 select count(distinct name, id) from A; --SQL Server和Access都不支持
示例4
select id, distinct name from A; --会提示错误,因为distinct必须放在开头
本文介绍了 SQL 中 DISTINCT 关键词的基本用法及其注意事项。DISTINCT 用于返回唯一不同的值,帮助用户去除查询结果中的重复记录。文章通过多个示例说明了如何使用 DISTINCT 来进行数据统计,并解释了其限制条件。
1758

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



