首页 > 解决方案 > 此版本的 MySQL 尚不支持 'LIMIT & IN/ALL/ANY/SOME 子查询'同时提取第 6 大薪水

问题描述

我编写了 Mysql 查询,该查询从员工表中提取了第 6 大的薪水。我浏览了其他堆栈溢出链接,也请不要将其设为重复。

我的查询是如何通过子查询中的限制。

下面的代码是内部连接工作正常

select e1.first_name,e1.salary 
from employees e1  
inner join (select employee_id 
            from employees  
            order by salary desc limit 6) as e2  
        on e1.employee_id = e2.employee_id  
order by salary asc limit 1;

| first_name | salary |
+------------+--------+
| Barby      | 164588 |

当我在子查询中写入时在子查询中传递限制时抛出错误

select first_name,salary 
from employees 
where employee_id in (select employee_id 
                      from employees  
                      order by salary desc **limit 6** ) 
order by salary desc limit 6;

| first_name | salary |
+------------+--------+
| Jacklyn    | 166976 |
| Carissa    | 166765 |
| Riley      | 166569 |
| Lauren     | 166016 |
| Lucy       | 165660 |
| Barby      | 164588 |

想要的结果在下面我想在子查询中写而不是加入

| first_name | salary |
+------------+--------+
| Barby      | 164588 |

标签: mysql

解决方案


推荐阅读