Home >Database >Mysql Tutorial >生成MySQL、SqlServer、Oracle数据的分页话语

生成MySQL、SqlServer、Oracle数据的分页话语

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-07 16:16:031127browse

生成MySQL、SqlServer、Oracle数据的分页语句 /*** @Title: getMySQLSql * @Description: 生成适合MySQL方言的SQL分页语句* @param sql * @param offset* @param limit* @return String */public String getMySQLSql(String sql, int offset, int limit) { re

生成MySQL、SqlServer、Oracle数据的分页语句

/**
* @Title: getMySQLSql 
* @Description: 生成适合MySQL方言的SQL分页语句
* @param  sql 
* @param  offset
* @param  limit
* @return String    
 */
public String getMySQLSql(String sql, int offset, int limit) {
   return sql + " limit " + offset + "," + limit;
}

/**
* @Title: getSqlServeSql 
* @Description: 生成适合SqlServer方言的SQL分页语句
* @param  sql 
* @param  pageNo
* @param  pageSize
* @return String    
 */
public String getSqlServeSql(String sql, int pageNo, int pageSize) {
  return "select top " + pageSize + " from (" + sql
	+ ") t where t.id not in (select top " + (pageNo-1)*pageSize + " t1.id 
		  from ("+ sql + ") t1)";
}

/**
* @Title: getOrcaleSql 
* @Description: 生成适合Oracle方言的SQL分页语句
* @param  sql 
* @param  pageNo
* @param  pageSize
* @return String    
 */
public String getOrcaleSql(String sql, int pageNo, int pageSize) {
   return "select * from (select rownum rn, t.* from (" + sql
		  + ") t where rownum <= " + (pageNo* pageSize)
		  + ") t1 where t1.rn > " + ((pageNo- 1) * pageSize);
}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn