SQL DISTINCT 用法(去重)

现在以下数据

查有出现的TeacherId 

1   SELECT  TeacherID FROM Student
View Code

结果

有重复的ID出现

确实查法:

1   SELECT TeacherID FROM Student
2   GROUP BY TeacherID
View Code
1 SELECT DISTINCT TeacherID FROM Student
View Code

结果:

 

可以看得出  DISTINCT 的作用就是去重

DISTINCT 还可以跟聚合函数使用: 

1   --两种查法得出的结果一样 都是统计有多少个不同的老师
2   SELECT COUNT( DISTINCT TeacherID) FROM Student
3 
4   SELECT COUNT(*) FROM ( SELECT DISTINCT TeacherID FROM Student) T
View Code

 

posted @ 2017-02-15 10:31  老糖  Views(8077)  Comments(0Edit  收藏  举报