Mysql子查询IN中使用LIMIT应用示例
程序员文章站
2024-02-28 12:23:34
这两天项目里出了一个问题,limit使用后报错。 需求是这样的,我有3张表,infor信息表,mconfig物料配置表,maaply物料申请表,要求是读出申请表中哪些人申请...
这两天项目里出了一个问题,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
)
结果报错了
当时没注意报的什么错误,只是看到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
)
<pre name="code" class="sql">limit 0,10</pre>
这样没有报错,莫离以为ok了,但是运行后发现,数据有问题
和单纯的读出申请表的内容不一样,才发现limit的位置放错了,于是又把limit发在in里,结果报错如下
this version of mysql doesn't yet support ‘limit & in/all/any/some subquery'
细看才知道,in里不支持limit。那怎么办呢?
于是度娘后得知,在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`
)
运行后,问题解决~~~
需求是这样的,我有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
)
结果报错了
当时没注意报的什么错误,只是看到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
)
<pre name="code" class="sql">limit 0,10</pre>
这样没有报错,莫离以为ok了,但是运行后发现,数据有问题
和单纯的读出申请表的内容不一样,才发现limit的位置放错了,于是又把limit发在in里,结果报错如下
this version of mysql doesn't yet support ‘limit & in/all/any/some subquery'
细看才知道,in里不支持limit。那怎么办呢?
于是度娘后得知,在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`
)
运行后,问题解决~~~
下一篇: Vuex 基本使用