今天在写 SQL 过程中,遇到 MySQL 报错:#1235 – This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY/SOME subquery’,错误信息比较明确,就是当前版本的 MySQL 不支持在子查询时使用 LIMIT 子句的 IN/ALL/ANY/SOME 子查询。解决方案就是再套一层查询

错误代码:

select * from students where id in (select id from students order by rand() limit 10);

正确代码:

select * from students where id in (select t.id from (select id from students order by rand() limit 10) as t);