《Mysql入门MySQL教程》要点:
本文介绍了Mysql入门MySQL教程,希望对您有用。如果有疑问,可以联系我们。
导读:本节内容:学习mysql子查询IN中LIMIT的用法.项目中遇到的问题:mysql limit使用后报错.需求是这样的,我有3张表,infor信息表,mconfig...
MYSQL学习本节内容:
学习mysql子查询IN中LIMIT的用法.
MYSQL学习项目中遇到的问题:mysql limit使用后报错.
MYSQL学习需求是这样的,我有3张表,infor信息表,mconfig物料配置表,maaply物料申请表,要求是读出申请表中哪些人申请哪些物料
MYSQL学习首先,这样:
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
)
MYSQL学习结果报错了.当时没注意报的什么错误,只是看到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
MYSQL学习这样没有报错,以为OK了,但是运行后发现,数据有问题
MYSQL学习和单纯的读出申请表的内容不一样,才发现LIMIT的位置放错了,于是又把LIMIT发在IN里,结果报错:
This version of MySQL doesn't yet support ‘LIMIT & IN/ALL/ANY/SOME subquery'
MYSQL学习细看才知道,IN里不支持LIMIT.如何解决呢?
MYSQL学习在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`
)
维易PHP培训学院每天发布《Mysql入门MySQL教程》等实战技能,PHP、MYSQL、LINUX、APP、JS,CSS全面培养人才。
转载请注明本页网址:
http://www.vephp.com/jiaocheng/12027.html