About Client interface Paging SQL statements

Source: Internet
Author: User

Today suddenly turned to SQL for the client to write paging data, found that the logic is not correct. Descending by ID

It was written in this way:

#翻上一页: Select field from table where Id>lastid ORDER by id DESC limit pagesize# turn next page: Select field from table where Id<lastid order b Y ID desc Limit pageSize

LastID is the near ID, that is, the previous page is the topmost ID, the next page is the bottom ID

PageSize is the number of page bars

Clearly, there is a problem with logic.

If the Pagesize=3;id list is like this:

Ten 9 8 7 6 5 4 3 2 1

Because the ID is descending, the page turn is like this:

1 First page:2 second page:  93 third page: 8  7   
...

But actually, the backend receives three parameters: Lastid,pagesize,direct. Direct=0 represents the previous page, 1 next page.

Lastid=9, and down a page, then id<9 Descending, get 8 7 6, no problem;

The previous page, then id>9, if only descending: 14 13 12, obviously should be ascending, that is: 10 11 12, and then descending, return to the client: 12 11 10

So It should be written .
1 #翻上一页:2 Select *  from 3(SelectField fromTablewhereId>LastIDOrder  byID limit pageSize)Temp4 Order  byIddesc5 #翻下一页:6 SelectField fromTablewhereId<LastIDOrder  byIddescLimit PageSize

About Client interface Paging SQL statements

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.