MYSQL:
SELECT * FROM book ORDER BY $sidx $sord LIMIT $start , $limit;
How do I turn this into Oracle?
You have to use rownum property, BUT you have to apply it to the result set already ordered AND you need to pass it from the inner query (credit to Bob Jarvis and Daniel Hilgarth: check their explanations and links given in comments as well). It should be something like this:
SELECT * FROM
(SELECT b.*, rownum as row_num FROM book b ORDER by $sidx $sord)
WHERE row_num BETWEEN $start AND $start + $limit;
SELECT * FROM (SELECT b.*, rownum as row_num from book b order by $sidx $sord) where row_num between $start and $start + $limit. You need to pull the rownum generated by the inner query out as a separate variable to make it visible by the outer query. Share and enjoy.where rownum > 1 there will be no rows returned as you would first need to return one row to have a rownum > 1. Your first link works as it is using rownum LESS THAN 50, i.e. the first 50 rows. Your fiddle works as it uses between one and three. Replace one with two and you will get no results. A bit hard to explain, that's why I linked authorative sources in my last comment.I am not familiar with the oracle syntax, but you can use the ROW_NUMBER() function to rank the rows then select those with rank BETWEEN @start and @limit:
SELECT *
FROM
(
select *,
ROW_NUMBER() OVER (ORDER BY ID DESC) AS rank
FROM book
)
WHERE rank BETWEEN @start AND @end
/* OR
WHERE BETWEEN ((@PageNum - 1) * @PageSize + 1)
AND (@PageNum * @PageSize)
/*
10g Onwards
WITH CTE AS(SELECT b.*, Row_Number() OVER(ORDER BY SIDX,SORD) AS Rn FROM BOOK b)
SELECT *
FROM CTE
WHERE Rn BETWEEN STARTLIMIT AND ENDLIMIT
searchbox at the top of the page to avoid downvotes and closes in future. FWIW, the canonical answer to the question "Oracle equivalent of LIMIT" is this question. Share and enjoy.