从MYSQL中的第三高薪中获取员工表中的所有员工薪水

时间:2017-12-25 17:02:09

标签: mysql

写下面的查询:

mysql > select * from employees where Salary NOT IN (select Salary from employees limit 3);

给出以下错误:

  

ERROR 1235(42000):此版本的MySQL尚不支持'LIMIT&   IN / ALL / ANY / SOME子查询'

2 个答案:

答案 0 :(得分:0)

您的查询中有2个问题。第一个是语法,第二个是LIMIT SELECT中没有ORDER BY。如果没有ORDER,结果可以每次都改变(Ramdom)

你可以使用这样的QUERY来做到这一点:

SELECT e.* FROM employees e WHERE e.salery NOT IN 
(SELECT * FROM (
    SELECT s.salery
    FROM employees s
    ORDER BY salery LIMIT 3) AS tmp)
ORDER BY e.salery ;

这是我的样本:

MariaDB [bernd]> select * FROM employees;
+----+--------+--------+
| id | salery | name   |
+----+--------+--------+
|  1 |    100 | Bernd  |
|  2 |    500 | Peter  |
|  3 |    300 | Paul   |
|  4 |   1234 | Mary   |
|  5 |    800 | Erwin  |
|  6 |    777 | Hubert |
+----+--------+--------+
6 rows in set (0.00 sec)

MariaDB [bernd]>

MariaDB [bernd]> SELECT e.* FROM employees e WHERE e.salery NOT IN
    -> (SELECT * FROM (
    ->     SELECT s.salery
    ->     FROM employees s
    ->     ORDER BY salery LIMIT 3) AS tmp)
    -> ORDER BY e.salery ;
+----+--------+--------+
| id | salery | name   |
+----+--------+--------+
|  6 |    777 | Hubert |
|  5 |    800 | Erwin  |
|  4 |   1234 | Mary   |
+----+--------+--------+
3 rows in set (0.00 sec)

答案 1 :(得分:0)

类似的东西:

select employees.*
from employees
where (
   select count(*) from employees as e
   where e.Salary >= employees.Salary
) > 3;

...但是根据表大小/索引,它可能对服务器来说太重了。 SQL小提琴:http://sqlfiddle.com/#!9/67754e/2