1

here's my data scructure:

news
id  date         name
----------------------
2   2015-12-01   news1
3   2015-12-04   news2 
4   2015-12-07   news3 
7   2015-12-08   news4 

i'm displaying the latest news record (id 7) and want to add navigation buttons for "older" and "newer"

so what's the best method for getting the url for the next older news record (id 4)? something like select * from news where [date which is next lower id to current id]

thanks

3 Answers 3

1

Query for "newer":

select * from news where date > (select date from news where id = X) order by date asc limit 1;

Query for "older":

select * from news where date < (select date from news where id = X) order by date desc limit 1;

Replace X with your target id. I assume the "id" is unique.

Tested on MySQL 5.5.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use limitwith range: The first argument is the offset, the second is how many items two return This will give you the first two records:

Select * from news order by id limit 0,2

And this the fillowing two:

Select * from news order by id limit 2,2

Comments

0

you can use below code parameter_date is date of record 7 in this example

//for newer : 
 SELECT * FROM news where date>parameter_date order by date asc limit 1,1;
//for older : 
  SELECT * FROM news where date<parameter_date order by date desc limit 1,1;

and if you want do this with id you can use below code parameter_id is your news id

//for newer : 
 SELECT * FROM news where date>(select date from news where id=parameter_id) order by date asc limit 1,1;
//for older : 
  SELECT * FROM news where date<(select date from news where id=parameter_id) order by date desc limit 1,1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.