简体   繁体   English

MySQL访问上一行的值

[英]MySQL accessing previous row values

I have two columns Page_from and Page_to . 我有两列Page_fromPage_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM