Extract the previous and next SQL statements from the database

Source: Internet
Author: User

We often use the previous one, the next, or the next in the CMS system, which is simple to use.

The tentative data table here is the news field name as ID. The current urlid parameter value is currentid.

If you follow the general idea, the following is the SQL statement used to extract the information from the previous article.

select * from news where id=currentId-1;

The following is the SQL statement used to extract the next information.

select * from news where id=currentId+1;

This is simple, but there is a limitation that all IDs must be connected. If disconnected, an error will occur, for example, if there is no id = 16 between id = 15 and ID = 17, an error occurs when you obtain the next record from id = 15 and ID = 17, therefore, the preceding SQL statement cannot be used.

The following is a good SQL statement used to process the previous and next operations.

Extract the previous SQL statement

select * from news where id<currentId order by id desc limit 0,1;

Or

select * from news where id<currentId order by id desc limit 1;

Extract the next SQL statement

select * from news where id >currrentId  order by id asc limit 0,1;

Or

select * from news where id >currrentId  order by id asc limit 1;    

I will not talk about hyperlink settings here. If you have any questions, please leave a message!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.