If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > LIMIT Clause for Oracle SQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Mar 2006
Posts: 2
LIMIT Clause for Oracle SQL

Hello, I'm developing web app with PHP and MySQL. And some how I also have to use Oracle DB. I'm just very new to Oracle/ Oracle SQL and have a problem to limit number of data to show for search result.

My problem is how to do search result pagenation with Oracle/Oracle SQL.
MySQL is simply use LIMIT clause.

I found out that I can use ROWNUM to limit search result to output.

So, I've tried following SQL statement.
Limit 20 per page and 25 total result for this example.

SELECT * FROM RECORD WHERE (ROWNUM >= 1 AND ROWNUM <= 20) AND AVAIL='Y" ORDER BY DATE DESC

Then netxt page query will be:
SELECT * FROM RECORD WHERE (ROWNUM >= 21 AND ROWNUM <= 25) AND AVAIL='Y" ORDER BY DATE DESC

But I don't get any result for second page.
Please someone help.
Reply With Quote
  #2 (permalink)  
Old
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
ROWNUM is applied after the results are selected, so it always starts from 1 and "WHERE ROWNUM >= 21" will never be true. You can achieve the effect you want like this:

Code:
SELECT a, b, c
FROM
( SELECT a, b, c, ROWNUM rn
  FROM
  ( SELECT a, b, c FROM RECORD 
    WHERE AVAIL='Y"
    ORDER BY DATE DESC
  )
  WHERE ROWNUM <= 25
)
WHERE rn >= 21;
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On