Topic: 分页显示的SQL语句应该如何写

  Print this page

1.分页显示的SQL语句应该如何写 Copy to clipboard
Posted by: wmgreat
Posted on: 2003-03-14 12:53

如果在第一页面显示1-10条记录,翻到第二页以后显示11-20条记录,第三野显示21-30,依次类推,请问SQL语句如果写才能只选择1-10条,11-20条,20-30等等?
请不要说用jdbc里面的定位函数,因为那样也是把所有的记录查出来再定位,效率不一定高!

2.Re:分页显示的SQL语句应该如何写 [Re: wmgreat] Copy to clipboard
Posted by: youlq
Posted on: 2003-03-14 13:31

1.
oracle 分页显示的sql

可以利用sql的rownum来完成这个工作

例:要取出20-40条记录
select * from table_name where rownum<41
minus
select * from table_name where rownum<20

2.用标准的SQL语句实现查询记录分页

http://www.javaresearch.org/article/showarticle.jsp?column=108&thread=2028

3.Re:分页显示的SQL语句应该如何写 [Re: wmgreat] Copy to clipboard
Posted by: why
Posted on: 2003-03-14 14:28

1. have to use ORDER BY in the above SELECT statements Smile
2. don't use MINUS, which is inefficient

3. suppose we want to get from record# startNum to endNum from a table "t" with some ordering criteria "k"

3.1 Oracle has ROWNUM, I think the example in the link youlq provided will work after making necessary corrections.

3.2 MySQL and PostgreSQL has LIMIT

3.3 SQL Server, Access, and perhaps FoxPro has "TOP n", try something like
SELECT TOP (endNum-startNum+1) *
FROM (SELECT TOP endNum * FROM t ORDER BY k DESC)
ORDER BY k ASC
(only guess, not tested)

3.4 DB2 has a TOP-n statement like
SELECT * FROM t FETCH FIRST n ROW ONLY
but I don't know whether it will work for nested SELECT as above...
may also try this function:
ROWNUM() OVER (ORDER BY k) AS rownumber
Questionsorry, can't remember the silly syntax.


BTW, I'm wondering whether a topic like this (I mean SQL) would be posted in 架构技术--Database instead. Smile

4.Re:分页显示的SQL语句应该如何写 [Re: why] Copy to clipboard
Posted by: youlq
Posted on: 2003-03-14 14:50

why wrote:
1. have to use ORDER BY in the above SELECT statements Smile
2. don't use MINUS, which is inefficient

3. suppose we want to get from startNum to endNum from a table "t" with some ordering criteria "k"
3.1 Oracle has ROWNUM, I think the example in the link youlq provided will work after making necessary corrections.
3.2 MySQL and PostgreSQL has LIMIT
3.3 SQL Server, Access, and perhaps FoxPro has "TOP n", try something like
SELECT TOP (endNum-startNum+1) *
FROM (SELECT TOP endNum * FROM t ORDER BY k DESC)
ORDER BY k ASC
(only guess, not tested)


说得好,呵呵!

5.Re:分页显示的SQL语句应该如何写 [Re: wmgreat] Copy to clipboard
Posted by: wmgreat
Posted on: 2003-03-14 18:27

why,怎一个佩服了得

6.试试上面的猜想 [Re: wmgreat] Copy to clipboard
Posted by: why
Posted on: 2003-03-14 20:35

wmgreat大侠:
小的实在只是瞎猜哦!

想不到这样也可以得分?惭愧-ingEmbaressed
找个 DB2 和 SQL Server 试试是否行得通……

SELECT TOP (endNum-startNum+1) *
FROM (SELECT TOP endNum * FROM t ORDER BY k DESC)
ORDER BY k ASC
logic 错了啦!现在是找倒数的 startNum 至 endNum 行Embaressed
语法亦错了,这样应该可以啦
SELECT tRANGE.*
FROM (SELECT TOP (endNum-startNum+1) tTOP.*
FROM (SELECT TOP endNum * FROM t ORDER BY k ASC) tTOP
ORDER BY tTOP.k DESC) tRANGE
ORDER BY tRANGE.k ASC

小的本来想用 Query Analyzer 看一看这个 SORT and SELECT 的方法和 MINUS 的效果(粗略地看看 SELECT FROM t 占的百分比),但却找不到 SQL Server上 MINUS 的语法,请各位大侠指点指点啦!

http://vyaskn.tripod.com/oracle_sql_server_differences_equivalents.htm 上指出
MINUS operator - Not supported
Use NOT EXISTS clause in your SELECT statement to generate the same result.

The following example illustrates the simulation of Oracle's MINUS operator:

SELECT OrderID, OrderDate
FROM Orders O
WHERE NOT EXISTS
(
SELECT 1
FROM RefundsTable R
WHERE O.OrderID = R.OrderID
)

又说
rownum pseudo column - No equivalent
Though there is no rownum or rowid in SQL Server, there are several ways in which a row number can be generated.

For some examples, read this article:
Q186133 INF: How to Dynamically Number Rows in a Select Statement


DB2 上用 FETCH FIRST n ROW ONLY 代替可行啦,即是
SELECT tRANGE.*
FROM (SELECT tTOP.*
FROM (SELECT * FROM t ORDER BY k ASC FETCH FIRST endNum ROW ONLY) tTOP
ORDER BY tTOP.k DESC
FETCH FIRST (endNum-startNum+1) ROW ONLY) tRANGE
ORDER BY tRANGE.k ASC


而 ROWNUM() OVER (ORDER BY k) AS rownumber 是记错了,正确的 function 是 ROWNUMBER() OVER (ORDER BY k)
http://www7b.boulder.ibm.com/dmdd/library/techarticle/adamache/0430_adamache3.html
SELECT tTOP.*
FROM (SELECT ROWNUMBER() OVER (ORDER BY k) AS rownum, t.*
FROM t ORDER BY k ASC
FETCH FIRST endNum ROW ONLY) tTOP
WHERE tTOP.rownum BETWEEN startNum AND endNum

7.Re:分页显示的SQL语句应该如何写 [Re: wmgreat] Copy to clipboard
Posted by: snowbug
Posted on: 2003-03-15 04:10

Oracle 用 order by 的话, rownum 还是对应着 order 以前的纪录,所以不是一个通用的解决办法。

我觉得用 JDBC 的 absolute() 是个很好的通用解决方法,而且大部分数据库都支持它,用起来也很方便。不过没有进行过完整的测试。谁有这样用过的经验?

8.Re:分页显示的SQL语句应该如何写 [Re: wmgreat] Copy to clipboard
Posted by: frankwater
Posted on: 2003-04-16 18:24

JDBC使用Statement的setMaxRows( )方法同时支持多个数据库。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923