【发布时间】:2026-02-03 18:05:02
【问题描述】:
已写如下查询:
mysql > select * from employees where Salary NOT IN (select Salary from employees limit 3);
给出以下错误:
ERROR 1235 (42000): 这个版本的 MySQL 还不支持 'LIMIT & IN/ALL/ANY/SOME 子查询'
【问题讨论】:
标签: mysql
已写如下查询:
mysql > select * from employees where Salary NOT IN (select Salary from employees limit 3);
给出以下错误:
ERROR 1235 (42000): 这个版本的 MySQL 还不支持 'LIMIT & IN/ALL/ANY/SOME 子查询'
【问题讨论】:
标签: mysql
您的查询中有 2 个问题。首先是语法,其次是 LIMIT SELECT 中没有 ORDER BY。如果没有 ORDER,则每次都可以更改结果(随机)
你可以用这样的查询来做到这一点:
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)
【讨论】:
类似:
select employees.*
from employees
where (
select count(*) from employees as e
where e.Salary >= employees.Salary
) > 3;
...但是根据表大小/索引,它可能对服务器来说太重了。 SQL 小提琴:http://sqlfiddle.com/#!9/67754e/2
【讨论】: