sql paging limit problem

The blogger's sql statement caused by carelessness has a bug in the use of the limit statement pagination. Let me share it with you.

SELECT * FROM t_complaint cc LEFT JOIN t_responsibility rr ON rr.`complaint_id` = cc.`pk_id` 
WHERE cc.is_delete=0 AND rr.`create_time` >= "2019-08-31 00:00:00" AND rr.`create_time` <= "2019-09-02 00:00:00" AND rr.`grade` =1 
ORDER BY cc.`complaintDate` DESC LIMIT 1,10

This is the blogger's sql statement. Because there are too many things, I directly select * instead, and ignore it here.

operation result

simon
In this way, the data cannot be found.After repeated tests by the blogger, it is found that the problem has occurred on my limit!

SELECT * FROM t_complaint cc LEFT JOIN t_responsibility rr ON rr.`complaint_id` = cc.`pk_id` 
WHERE cc.is_delete=0 AND rr.`create_time` >= "2019-08-31 00:00:00" AND rr.`create_time` <= "2019-09-02 00:00:00" AND rr.`grade` =1 
ORDER BY cc.`complaintDate` DESC 

Take out the running result of limit

simon
It can be seen that you can get the results you want by removing the limit, but you want to use sql paging and can not remove the limit, then what is the reason at this time, the blogger was also puzzled at the time, wrote So many times the sql pagination has a problem on this for the first time, after a bit of testing, I found that you can change the limit to 0, 10 and you can get the result you want, so why is it! The blogger suddenly remembered, * The formula for calculating the total number of pagination is: (Page -1) The number displayed on the current page ; it immediately responds that I forgot to deal with it! The blogger judges and handles it this way:
if (pageSize>=10){ pageSize = 10; }if (pageNum == 1){ pageNum = 0; }else { pageNum=(pageNum-1)*pageSize; }
Of course, the blogger is a thinking person, and this problem bloggers also a lot of online searching for information: use the limit of stress or find many
limit order must meet to use, otherwise there will be duplication of data
in this example a blogger is not posted, interested can see for yourself!
with a limit must First order by ~ The order is ------ first order by… then limit…
There is still a lot of attention to limit, bloggers here remind programmers to pay attention!

Published 34 original articles · won praise 0 · Views 3634

Guess you like

Origin blog.csdn.net/qq_43469899/article/details/100192347