mysql的distinct和group by
事情起因是从一次sql查询引发的:
找出所有员工当前(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`));
这道题有一个比较统一的解法:(select .. distinct+order by)
select distinct salary from salaries where to_date='9999-01-01' order by salary desc;
但是别人给出另一种方案(select .. group by order by)
使用分组语句去重
比较两种解法发现:第二种方案在大数据量前提下查询效率更高一点
select salary from salaries where to_date='9999-01-01' group by salary order by salary desc;
这里贴出别人做的实验连接:https://blog.csdn.net/qq_36670734/article/details/105844908