您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Mysql子查询IN中使用LIMIT_MySQL

2025/4/9 9:38:24发布26次查看
bitscn.com
学习下mysql子查询in中使用limit的方法。
这两天项目里出了一个问题,mysql limit使用后报错。
需求是这样的,我有3张表,infor信息表,mconfig物料配置表,maaply物料申请表,要求是读出申请表中哪些人申请哪些物料
于是,首先这样写:
select infor.name,infor.phone,infor.add,
mconfig.mname,mapply.acount,from_unixtime(mapply.atime,'%y-%m-%d') as 'atime'
from mapply right join infor on mapply.uid = infor.uid inner join mconfig on mapply.mid = mconfig.mid
where mapply.aid
in (
select aid
from `mapply` where state = $state
order by `atime` , `uid` desc
limit 0,10
) www.jbxue.com
结果报错了
当时没注意报的什么错误,只是看到limit什么的错误,于是修改以下代码:
select infor.name,infor.phone,infor.add,
mconfig.mname,mapply.acount,from_unixtime(mapply.atime,'%y-%m-%d') as 'atime'
from mapply right join infor on mapply.uid = infor.uid inner join mconfig on mapply.mid = mconfig.mid
where mapply.aid
in (
select aid
from `mapply` where state = $state
order by `atime` , `uid` desc
)
limit 0,10
这样没有报错,莫离以为ok了,但是运行后发现,数据有问题
和单纯的读出申请表的内容不一样,才发现limit的位置放错了,于是又把limit发在in里,结果报错如下
this version of mysql doesn't yet support ‘limit & in/all/any/some subquery'
细看才知道,in里不支持limit。那怎么办呢?www.jbxue.com
于是度娘后得知,在in里再使用一张临时表,把需要的内容先查出来,
修改后:
select infor.name,infor.phone,infor.add,
mconfig.mname,mapply.acount,from_unixtime(mapply.atime,'%y-%m-%d') as 'atime'
from mapply right join infor on mapply.uid = infor.uid inner join mconfig on mapply.mid = mconfig.mid
where mapply.aid
in (
select aid
from (select `aid` from `mapply` where state = $state
order by `atime` , `uid` desc
limit 0,10)as `tp`
)
运行后,问题解决!
bitscn.com
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product