SQL DISTINCT 用法(去重)
现在以下数据
查有出现的TeacherId
1 SELECT TeacherID FROM Student
结果
有重复的ID出现
确实查法:
1 SELECT TeacherID FROM Student 2 GROUP BY TeacherID
1 SELECT DISTINCT TeacherID FROM Student
结果:
可以看得出 DISTINCT 的作用就是去重
DISTINCT 还可以跟聚合函数使用:
1 --两种查法得出的结果一样 都是统计有多少个不同的老师 2 SELECT COUNT( DISTINCT TeacherID) FROM Student 3 4 SELECT COUNT(*) FROM ( SELECT DISTINCT TeacherID FROM Student) T