MySQL不用order by实现排名的3种思路

本文介绍了三种在MySQL中不使用ORDER BY子句来查找在职员工薪资第二名的方法。首先,通过基础解法,先查询最高薪资再筛选出第二高;其次,利用自联结查询,通过计数找到薪资次高的员工;最后,提供了一个更简洁的自联结查询优化版。所有方法均成功返回薪资第二高的员工信息。
摘要由CSDN通过智能技术生成

MySQL不用order by实现排名的3种思路

  • 假定业务:

    查看在职员工的薪资的第二名的员工信息

  • 创建数据库
    drop database if exists emps;
    create database emps;
    use emps;
    
    create table employees(
        empId int primary key,-- 员工编号
        gender char(1) NOT NULL, -- 员工性别
    	hire_date date NOT NULL -- 员工入职时间
       	);
    create table salaries(
        empId int primary key, 
        salary double -- 员工薪资
        );
        
    INSERT INTO employees VALUES(10001,'M','1986-06-26');
    INSERT INTO employees VALUES(10002,'F','1985-11-21');
    INSERT INTO employees VALUES(10003,'M','1986-08-28');
    INSERT INTO employees VALUES(10004,'M','1986-12-01');
    INSERT INTO salaries VALUES(10001,88958);
    INSERT INTO salaries VALUES(10002,72527);
    INSERT INTO salaries VALUES(10003,43311);
    INSERT INTO salaries VALUES(10004,74057);
    
  • 题解思路

    1. (基础解法)

      先查出salaries表中最高薪资,再以此为条件查出第二高的工资

      查询语句如下:

    select
    	E.empId,E.gender,E.hire_date,S.salary
    from
    	employees E join salaries S 
    on 
    	E.empId = S.empId
    where	
    	S.salary=
    	(
        select max(salary)from salaries 
        where 
            salary<
            (select max(salary) from salaries)
        );
    -- ---------------查询结果------------ --
    +-------+--------+------------+--------+
    | empId | gender | hire_date  | salary |
    +-------+--------+------------+--------+
    | 10004 | M      | 1986-12-01 |  74057 |
    +-------+--------+------------+--------+
    
    1. (自联结查询)

      先对salaries进行自联结查询,当s1<=s2链接并以s1.salary分组,此时count的值,即薪资比他高的人数,用having筛选count=2 的人,就可以得到第二高的薪资了;

      查询语句如下:

    select
    	E.empId,E.gender,E.hire_date,S.salary
    from
    	employees E join salaries S 
    on 
    	E.empId = S.empId
    where S.salary=
    	(
        select 
            s1.salary
        from 
            salaries s1 join salaries s2 
        on 
            s1.salary <= s2.salary
        group by 
            s1.salary              
      	having
      	 count(distinct s2.salary) = 2
        );
    -- ---------------查询结果------------ --
    +-------+--------+------------+--------+
    | empId | gender | hire_date  | salary |
    +-------+--------+------------+--------+
    | 10004 | M      | 1986-12-01 |  74057 |
    +-------+--------+------------+--------+
    
    1. (自联结查询优化版)

      原理和2相同,但是代码精简了很多,上面两种是为了引出最后这种方法,在很多时候group by和order by都有其局限性,对于俺们初学者掌握这种实用性较广的思路,还是很有意义的。

    select
    	E.empId,E.gender,E.hire_date,S.salary
    from
    	employees E join salaries S 
    on
        S.empId =E.empId
    where
        (select count(1) from salaries where salary>=S.salary)=2;
    -- ---------------查询结果------------ --
    +-------+--------+------------+--------+
    | empId | gender | hire_date  | salary |
    +-------+--------+------------+--------+
    | 10004 | M      | 1986-12-01 |  74057 |
    +-------+--------+------------+--------+
    
  • 初浅总结,如有错误,还望指正。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodePhage

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值