1. 程式人生 > >oracle VS mysql 的分頁查詢

oracle VS mysql 的分頁查詢

首先是Oracle:

String sql = " select * from ( select t.*,rownum as num from (select * from user1  where 1=1 ";

Set<Entry<String, Object>> set = m.entrySet();
Iterator io = set.iterator();
while (io.hasNext()) {
Map.Entry<String, Object> me = (Map.Entry<String, Object>) io.next();

if("username".equals(me.getKey()) && !"".equals(me.getValue())){
sql += " and " + me.getKey() + " like '%"+ me.getValue()  +"%'" ;
}
if("startTime".equals(me.getKey()) && !"".equals(me.getValue())){
sql += " and " + me.getKey() + " >= '" + me.getValue() +"'";
}
if("endTime".equals(me.getKey()) && !"".equals(me.getValue())){
sql += " and " + me.getKey() + " <= '" + me.getValue() +"'";
}
if("sort".equals(me.getKey()) && !"".equals(me.getValue())){
sql += " order by " + me.getValue() ;
}
if("order".equals(me.getKey()) && !"".equals(me.getValue())){
sql += " " + me.getValue();
}
}
sql += " )t) " ;
sql = sql +" where num >=" + (currentPage-1)*pageSize +" and  num<="  + currentPage*pageSize+"" ;


  接著是MySQL:

String sql = " select * from user  where 1=1 ";

Set<Entry<String, Object>> set = m.entrySet();
Iterator io = set.iterator();
while (io.hasNext()) {
Map.Entry<String, Object> me = (Map.Entry<String, Object>) io.next();
if("username".equals(me.getKey()) && !"".equals(me.getValue())){
sql += " and " + me.getKey() + " like '%"+ me.getValue()  +"%'" ;
}
if("startTime".equals(me.getKey()) && !"".equals(me.getValue())){
sql += " and " + me.getKey() + " >= '" + me.getValue() +"'";
}
if("endTime".equals(me.getKey()) && !"".equals(me.getValue())){
sql += " and " + me.getKey() + " <= '" + me.getValue() +"'";
}
if("sort".equals(me.getKey()) && !"".equals(me.getValue())){
sql += " order by " + me.getValue() ;
}
if("order".equals(me.getKey()) && !"".equals(me.getValue())){
sql += " " + me.getValue();
}
}
sql = sql +" limit " + (currentPage-1)*pageSize +" , "  + pageSize ;

pageSize 該頁有多少條資料,currentPage該頁碼