题解 | #查询结果去重#
查询结果去重
https://www.nowcoder.com/practice/82ebd89f12cf48efba0fecb392e193dd
1.使用distinct
select distinct university from user_profile;
2.使用group by
select university from user_profile group by university;
distinct的含义及用法
①含义
distinct用来返回不重复字段的条数,其原因是distinct只能返回他的目标字段,而无法返回其他字段。
②用法
- 只对一个字段查重:
select distinct [查询字段] from 表名
- 多个字段去重:
select distinct [查询字段1][查询字段2] from 表名
-
所有查询字段均按照distinct字段取单条:
(1)group by
(2)group_concat -
聚合函数中使用distinct:一般跟 COUNT 结合使用, count()会过滤掉null项
查看20道真题和解析