Mysql:Error Code 1235,This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

前端之家收集整理的这篇文章主要介绍了Mysql:Error Code 1235,This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

执行sql语句遇到错误

[Err] 1235 - This version of MysqL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

sql语句:

SELECT id,friend_id,sender_id,obj_key,comment_id,content,ctime from msg 
where  
id in (SELECT MAX(id) as id FROM msg WHERE user_id = 312 and status !=3  GROUP BY obj_key,comment_id ORDER BY id DESC LIMIT 10)

结果查询发现是:在MysqL中子查询是不能使用LIMIT

解决办法:

比如这样的语句是不能正确执行的。 

select * from table where id in (select id from table limit 12);

第一种方法:只要你再加一层就行。如: 

select * from table where id in (select t.id from (select * from table limit 12) as t)

第二种方法

select * from (select id from table limit 12) as t;

第三种方法:使用inner join ....  on ....

select t.id from table as t 
inner join
(select id from table limit 12) as t2
on t.id=t2.id

建议使用inner join,因为通常db引擎会将where 解析为join

参考:http://blog.chinaunix.net/uid-22414998-id-2945656.html

猜你在找的MySQL相关文章