多层limit查询 mysql出现异常This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

本文探讨了在MySQL中如何正确使用LIMIT结合子查询的方法,解决了因语法限制导致的查询错误,并给出了修正后的SQL语句示例。

This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'

这个版本的MySQL还不支持的限制/所有/任何/查询”

原始的sql如下:

select  * from 
(select   pricerecord.* from pricerecord    where 1=1  and zt=0 and 
pname in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,66,67)   
order by pdate desc limit 30) as c
where pid not in  
( select  pricerecord.pid pid from pricerecord  where 1=1  and zt=0 and pname in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,66,67)    
order by pdate desc limit 1 ) order by pdate desc limit 30

这段代码可以简化一下,大概是
select  * from 
(select   t1.* from pricerecord t1   where 1=1  and zt=0 and 
pname in (1,2)   order by pdate desc limit 30) as c
where pid not in  
( select  pricerecord.pid pid from pricerecord  where 1=1  and zt=0 and pname in (1,2)    
order by pdate desc limit 1 ) order by pdate desc limit 30
这样,其实就是查询一个表的所有字段,其中,表是 通过第一个查询语句查询出来的,取别名叫做c,条件是pid not in查询的not in集合是一个查询条件,同样有limit,此时,两个子句单独使用查询都没有问题,但是一起执行出现以上错误,参考 此篇文章  可以看到,其实他是在第二个条件语句上再加上了一层查询

其实上述语句更改成如下更易看出问题

select * from tablename where pid not (select pricerecord.pid from pricerecord where 1=1 and zt=0 and pname in (1,2) order by pdate desc limit 1) order by pdate desc limit 30   

应该更改为:

select * from tablename where pid not(select temp.pid from (select pricerecord.pid pid from pricerecord where 1=1 and zt=0 and pname in (1,2) order by pdate desc limit 1)as temp) order by pdate desc limit 30   

变种为

select  * from 
(select   pricerecord.* from pricerecord    where 1=1  and zt=0 and 
pname in (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,66,67)   
order by pdate desc limit 30) as c  where pid not (select temp.pid from (select pricerecord.pid pid from pricerecord where 1=1 and zt=0 and pname in (1,2) order by pdate desc limit 1)as temp) order by pdate desc limit 30   


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值