Liny_@NotePad

沉迷ACG中

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

YOYO posted @ 2009年10月27日 06:25 in 【数据库】 with tags top LIMIT MS-SQL mysql , 5340 阅读

同学的一个项目。。之前的数据库是MSSQL的,满目的top在子查询语句中,于是照猫画虎,把top和pageSize放到后面改成limit,发现报错。

这个错误的意思是,该版本的MySQL不支持LIMIT在IN/ALL/ANY/SOME的子句中。

其实 = = limit函数已经很好用了。。比如原来的MSSQL用的拼接语句是

  1.                 sqlStr = "select top " + pageSize + " * from IndentInfo";
  2.                 if (page == 1)
  3.                 {
  4.                         sqlStr = sqlStr + " order by Id desc";
  5.                 }else {
  6.                         sqlStr = sqlStr + " where Id not in ( select top " + (recordCount-pageSize * page ) + " Id from IndentInfo order by Id
  7. ) and Id in " +
  8.                         "(select  top " + (recordCount - pageSize * (page-1)) + " Id from IndentInfo  order by Id) " + " order by Id desc";
  9.                 }

改为MySQL用的拼接语句则可以是:

  1.                 sqlStr = "select * from IndentInfo";
  2.                 if (page == 1)
  3.                 {
  4.                         sqlStr = sqlStr + " order by Id desc limit " + pageSize;
  5.                 }else {
  6.                         sqlStr = sqlStr + " order by Id desc limit " + (recordCount - pageSize * (page-1)) + ", " + pageSize;
  7.                 }

limit中的参数如果只有1个,则表示页面大小;如果有2个,则第一个参数为起始记录,第二个参数为页面大小。


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter