关于SQL中distinct的用法的注意

给个例题:
找出所有员工当前(to_date=‘9999-01-01’)具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示
CREATE TABLE salaries (
emp_no int(11) NOT NULL,
salary int(11) NOT NULL,
from_date date NOT NULL,
to_date date NOT NULL,
PRIMARY KEY (emp_no,from_date));

这个题用distinct做挺简单的:

select distinct salary 
from salaries where to_date = '9999-01-01' 
order by salary desc

但是看到评论,我知道了使用distinct的缺点:
对于distinct与group by的使用:
1、当对系统的性能高并数据量大时使用group by
2、当对系统的性能不高时使用数据量少时两者皆可
3、尽量使用group by

所以,该用group by

select salary from salaries 
where to_date = '9999-01-01' 
group by salary order by salary desc

猜你喜欢

转载自blog.csdn.net/weixin_43763859/article/details/106743886