[英]MySQL accessing previous row values
I have two columns Page_from
and Page_to
. 我有两列
Page_from
和Page_to
。 When the records are sorted, they appear as... 记录排序后,它们显示为...
Page_from Page_to
1 4
5 7
9 11
Here page number 8 is missing. 这里缺少第8页。 I want to find the missing page number.So I must be able to compare the value of
Page_to
in previous row with Page_from
in current row. 我想找到缺页number.So我必须能够价值比较
Page_to
与上一行Page_from
当前排。
You can find the beginning of a missing sequence by finding the previous record, and comparing the previous page_to
and the current page_from
. 您可以通过找到先前的记录,然后将先前的
page_to
和当前的page_from
进行比较,来找到缺失序列的开始。 If there is a gap, you can get both the first and last page in the gap. 如果存在间隙,则可以同时获得第一页和最后一页。
select tprev.page_to + 1 as missing_page_from, t.page_from - 1 as missing_page_to
from (select t.*,
(select tprev.page_to
from t tprev
where tprev.page_from < t.page_from
limit 1
) as prev_to
from t
) t
where prev_to is not null and
prev_to <> t.page_from - 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.