griegs
griegs

Reputation: 22770

Get next record in dataset from guid

I have a table that has say four fields in it.

guid, date, group, description

So I get a record set based on group and then ordered by date and say that nets me three records.

I select the first record and show it in my web page. I have next and previous buttons.

When I click on the next button, how do I get the next record in the record set given there is no row identifier such as 1,2,3 etc?

Is there a sql way to get the previous and next records or do I need to iterate through my record list looking for the guid and showing the next record?

Upvotes: 1

Views: 171

Answers (1)

Douglas Lise
Douglas Lise

Reputation: 1486

If you show only one record, you could consider you are paginating your records. In this case you have only one record per page.

Most of SQL engines, except Oracle, afaik, supports LIMIT and OFFSET functions, that are commonly used for paginating purposes.

LIMIT specifies the amount of records you want to fetch.
OFFSET specifies the starting record you want to fetch.

You must put these keys at the end of your SQL, like this:

Select guid, date, group, description
  From Your_Table
 Where Group = 'YourGroup'
 Order By Date
 Limit 1 Offset 0

And in your application you could store the current "page", so when the user clicks on Next then you increment the "page" variable and pass it to the Offset keyword of your SQL. Or decrement it when the user clicks on Back.

Upvotes: 1

Related Questions