Mysql子查询IN中使用LIMIT应用示例_MySQL
程序员文章站
2024-01-07 10:00:10
...
bitsCN.com
这两天项目里出了一个问题,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
)
这样没有报错,莫离以为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`
)
运行后,问题解决~~~bitsCN.com
需求是这样的,我有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
)
LIMIT 0,10
这样没有报错,莫离以为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`
)
运行后,问题解决~~~bitsCN.com
推荐阅读
-
MySQL中连接查询和子查询的问题
-
MySql中关于正则表达式查询的使用方法介绍
-
MySQL查询中LIMIT的大offset导致性能低下浅析
-
使用distinct在mysql中查询多条不重复记录值的解决办法
-
MySQL子查询中order by不生效问题的解决方法
-
Mysql中where、group by、having、Order by和limit五种子句使用介绍
-
PHP使用mysql_fetch_object从查询结果中获取对象集的方法_PHP教程
-
如何使用MySQL查询某个列中相同值的数量统计_MySQL
-
mysql中逻辑查询处理的介绍(代码示例)
-
mysql unique key在查询中的使用与相关问题