Home > database >  Mysql paging query problem
Mysql paging query problem

Time:09-16

I have 6 data, it is necessary to find out 2 pages of data, data, each page and sorted according to fenshu field:
1. (SELECT * from grade a to ORDER BY a.f enshu desc) LIMIT 2, 2;
2. (SELECT * from grade a to ORDER BY a.f enshu) LIMIT 2, 2;
3. SELECT * from grade a to ORDER BY a.f enshu asc LIMIT 2, 2;

But 1 2 found out the result is the same, 3 query results are different, this is what reason, have a great god to explain? Thank you

CodePudding user response:

First of all, you want to clear tell your demand, I looked at it and did you mean it, exist in A database table 6 data, now you want to according to the SQL to be divided into three pages, each page 2, and need the data in table A installation shuju field sorting??

CodePudding user response:

If so, I give the following solutions: (1) make sure the sorting way, such as you want to install the scores from high to low order (2) you did not give a specific table structure, I give you to create a,
Table structure is: the name of the table student: field has id, name, grade, corresponding table id, name, score
Finally: (1) the first page of the SQL below the select s.n ame, s.g rade from student s
The order by s.g rade desc
Limit 0, 2
(2) the second page the following SQL select s.n ame, s.g rade from student s
The order by s.g rade desc
Limit 2
(2) the third page of SQL as follows the select s.n ame, s.g rade from student s
The order by s.g rade desc
Limit of 4, 2

CodePudding user response:

If say these in fact you know, it is to limit the statement you understand thoroughly, before taking the query article n data, to limit 0, n), can also be used to limit n

CodePudding user response:

SELECT * from grade a to ORDER BY a.f enshu asc LIMIT (pageNum - 1) * pageSize, pageSize.
Pages should be like this, the first parameter is the current page number 1 times article page number, the second parameter is a number of each page,
  • Related