Posted in 面试题 onSeptember 29, 2012
1.Oracle:
select * from ( select row_.*, rownum rownum_ from ( query_SQL ) row_ where rownum == min
2.SQL Server:
select top @pagesize * from tablename where id not in (select top @pagesize*(@page-1) id from tablename order by id) order by id
3.MySQL
select * from tablename limit position, counter
4.DB2
select * from (select *,rownumber() as ROW_NEXT from tablename) where ROW_NEXT between min and max
——————————————————————————————–
1.分页方案一:(利用Not In和SELECT TOP分页)效率次之
语句形式:
SELECT TOP 10 * FROM TestTable
WHERE(ID NOT IN (SELECT TOP 20 id FROM TestTable ORDERBY id)) ORDERBYID
SELECT TOP 页大小 * FROM TestTable
WHERE( ID NOT IN (SELECT TOP 每页大小-1*待查询页数-1 id FROM 表 ORDERBY id)) ORDERBYID
思路:先查询出待查询页之前的全部条数的id,查询ID不在这些ID中的指定数量条数
2.分页方案二:(利用ID大于多少和SELECT TOP分页)效率最高
语句形式:
SELECT TOP 10 * FROM TestTable
WHERE(ID>(SELECT MAX(id) FROM(SELECT TOP20 id FROM TestTable ORDERBYid)AS T))ORDERBY ID
SELECT TOP 页大小* FROM TestTable
WHERE(ID>(SELECT MAX(id) FROM(SELECT TOP 每页大小*待查询页数-1 id FROM 表 ORDERBY id)AS T)) ORDERBY ID
思路:先获得待查询页的之前全部条数id,获得它们当中最大的ID号,以此最大ID号为标志,查找比这个ID号大的指定条数
3.分页方案三:
SELECT TOP PageSize * FROM(SELECT TOP nPage*PageSize * from YOURTABLE order by id)as a order by id desc
SELECT TOP 每页条数 * FROM (SELECT TOP 待查询页*每页条数) * from YOURTABLE order by id)as a order by id desc
思路:先正排序查询出待查询页之前(包括当前页)的全部条数,然后将其倒排序,取指定条数
select * from ( select row_.*, rownum rownum_ from ( query_SQL ) row_ where rownum == min
2.SQL Server:
select top @pagesize * from tablename where id not in (select top @pagesize*(@page-1) id from tablename order by id) order by id
3.MySQL
select * from tablename limit position, counter
4.DB2
select * from (select *,rownumber() as ROW_NEXT from tablename) where ROW_NEXT between min and max
——————————————————————————————–
1.分页方案一:(利用Not In和SELECT TOP分页)效率次之
语句形式:
SELECT TOP 10 * FROM TestTable
WHERE(ID NOT IN (SELECT TOP 20 id FROM TestTable ORDERBY id)) ORDERBYID
SELECT TOP 页大小 * FROM TestTable
WHERE( ID NOT IN (SELECT TOP 每页大小-1*待查询页数-1 id FROM 表 ORDERBY id)) ORDERBYID
思路:先查询出待查询页之前的全部条数的id,查询ID不在这些ID中的指定数量条数
2.分页方案二:(利用ID大于多少和SELECT TOP分页)效率最高
语句形式:
SELECT TOP 10 * FROM TestTable
WHERE(ID>(SELECT MAX(id) FROM(SELECT TOP20 id FROM TestTable ORDERBYid)AS T))ORDERBY ID
SELECT TOP 页大小* FROM TestTable
WHERE(ID>(SELECT MAX(id) FROM(SELECT TOP 每页大小*待查询页数-1 id FROM 表 ORDERBY id)AS T)) ORDERBY ID
思路:先获得待查询页的之前全部条数id,获得它们当中最大的ID号,以此最大ID号为标志,查找比这个ID号大的指定条数
3.分页方案三:
SELECT TOP PageSize * FROM(SELECT TOP nPage*PageSize * from YOURTABLE order by id)as a order by id desc
SELECT TOP 每页条数 * FROM (SELECT TOP 待查询页*每页条数) * from YOURTABLE order by id)as a order by id desc
思路:先正排序查询出待查询页之前(包括当前页)的全部条数,然后将其倒排序,取指定条数
说一下mysql, oracle等常见数据库的分页实现方案
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Tags in this post...
Reply on: @reply_date@
@reply_contents@