oracle,sqlserver分页查询sql语句
2011-08-30 16:17
302 查看
Oracle:
方法一:
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM (SELECT * FROM TABLE_NAME) A
WHERE ROWNUM <= 40
)
WHERE RN >= 21;
方法二:
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM (SELECT * FROM TABLE_NAME) A
)
WHERE RN between 21 and 40
公认第二种方法效率没有第一种高。原因是第二种要把子查询执行完,而第一种方法子查询执行到Rownum=40后就结束了。
MySQL:
SELECT * FROM TABLE_NAME LIMIT 10, 20
表示从第11条数据开始取20条数据返回,limit后的2个参数含义为:起点和步长,即从那条数据开始,取多少条数据,再如取前20条数据:SELECT * FROM TABLE_NAME LIMIT 0, 20
SQL Server2000:
SELECT TOP @pagesize * FROM TABLE_NAME WHERE id not in (SELECT TOP @pagesize*(@page-1) id FROM TABLE_NAME ORDER BY id) ORDER BY id
SQL Server 2005:
方法一:
SELECT ...
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY ID asc) AS RowNum, ......
FROM TABLE_NAME
) AS T
WHERE T.RowNum> 10 and T.RowNum<= 20
方法二:
WITH DataList AS
(
SELECT ROW_NUMBER() OVER (ORDER BY O.ID DESC)AS RowNum, ......
FROM .....
WHERE ......
)
SELECT ......
FROM DataList
WHERE RowNum BETWEEN 10 AND 20
方法一:
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM (SELECT * FROM TABLE_NAME) A
WHERE ROWNUM <= 40
)
WHERE RN >= 21;
方法二:
SELECT * FROM
(
SELECT A.*, ROWNUM RN
FROM (SELECT * FROM TABLE_NAME) A
)
WHERE RN between 21 and 40
公认第二种方法效率没有第一种高。原因是第二种要把子查询执行完,而第一种方法子查询执行到Rownum=40后就结束了。
MySQL:
SELECT * FROM TABLE_NAME LIMIT 10, 20
表示从第11条数据开始取20条数据返回,limit后的2个参数含义为:起点和步长,即从那条数据开始,取多少条数据,再如取前20条数据:SELECT * FROM TABLE_NAME LIMIT 0, 20
SQL Server2000:
SELECT TOP @pagesize * FROM TABLE_NAME WHERE id not in (SELECT TOP @pagesize*(@page-1) id FROM TABLE_NAME ORDER BY id) ORDER BY id
SQL Server 2005:
方法一:
SELECT ...
FROM
(
SELECT ROW_NUMBER() OVER (ORDER BY ID asc) AS RowNum, ......
FROM TABLE_NAME
) AS T
WHERE T.RowNum> 10 and T.RowNum<= 20
方法二:
WITH DataList AS
(
SELECT ROW_NUMBER() OVER (ORDER BY O.ID DESC)AS RowNum, ......
FROM .....
WHERE ......
)
SELECT ......
FROM DataList
WHERE RowNum BETWEEN 10 AND 20
相关文章推荐
- Oracle sql"NOT IN"语句优化,查询A表有、B表没有的数据
- oracle 实时查询最耗CPU资源的SQL语句
- Oracle分组查询结果SQL语句
- oracle12c JSON数据 使用SQL语句实现多表左外连接 显示无效数据 分页查询
- oracle与mysql分页查询语句sql
- 查询Oracle正在执行的sql语句
- Oracle 查询并删除重复记录的SQL语句
- Mybatis中使用oracle的模糊查询的SQL语句写法
- 查询oracle最耗资源的sql语句
- oracle的一些常用的查询的sql语句
- oracle 日期查询SQL语句
- Oracle--查询正在执行和执行过的SQL语句及语句执行性能
- 查询Oracle正在执行的sql语句,锁表,解锁
- 怎么使用robomongo查询mongodb(就像用plsql,输入sql语句,查询oracle数据一样)?
- Oracle实现查询时间段的Sql语句两法
- Oracle中查询某字段不为空或者为空的SQL语句怎么写
- 关于oracle sql语句查询时 表名和字段名要加双引號的问题具体解释
- oracle sql语句复杂查询
- Oracle 10g SQL分页查询语句和效率分析
- Oracle 常用SQL查询语句