I am trying to implement pagination to a page on my website that returns results from a database table.
Currently, it returns all rows in a random order. However, as my database is growing, I want to paginate these results instead of displaying them all on one page. However, I don't want to return all results just to display 20 records for instance. Depending on the page, I want take just the 20 records from the database that are relevant.
I'm following this tutorial: Tutorial
However, the I cannot use the query with the OFFSET
clause, because the hosting uses SQL SERVER 2008. (It is introduced in 2012 i believe).
I tried following the answer to this Question, but I want the results in a random order, and I cannot do an ORDER BY
on a derived table... so I'm a bit stuck for ideas!
Any help? Thanks!
This is what I currently have:
SELECT Title, Filename, PhotoURL, Orientation, FolderName, SetURL, RowNum
FROM (
SELECT p.Title, p.Filename, p.URL AS PhotoURL, p.Orientation, s.FolderName, s.URL AS SetURL, ROW_NUMBER() OVER (ORDER BY p.PhotoID) AS RowNum
FROM Photos p
LEFT OUTER JOIN SetPhotos sp
ON sp.PhotoID = p.PhotoID
LEFT OUTER JOIN [Sets] s
ON s.SetID = sp.SetID
WHERE p.Hide = 0
ORDER BY NEWID()
) AS PaginatedPhotos
WHERE PaginatedPhotos.RowNum BETWEEN 0 AND 10