首页 >数据库 >mysql教程 >生成MySQL、SqlServer、Oracle数据的分页话语

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB原创
2016-06-07 16:16:031125浏览

生成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);
}

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn