[英]Get missing numbers from mysql database between two numbers
I'd like to get the missing numbers from a mysql database between two numbers.我想从两个数字之间的 mysql 数据库中获取缺失的数字。 The range is from 0000000001 to 9999999999. In my table I have two columns serial_start
and serial_end
.范围是从 0000000001 到 9999999999。在我的表中,我有两列serial_start
和serial_end
。 In the first row, values are 100 and 500 and in the second row, 501 and 9999999999. I want to get the missing values from 1 to 99.第一行的值为 100 和 500,第二行的值为 501 和 9999999999。我想获取 1 到 99 之间的缺失值。
![enter image description here][1] ![在此处输入图像描述][1]
You can use lead()
to get the missing values in-between rows and union all
to bring in the first one:您可以使用lead()
来获取行之间的缺失值,并union all
以引入第一个:
select serial_start + 1, next_ss - 1
from (select t.*,
lead(serial_start) over (partition by year order by serial_start) as next_ss
from t
where year = 2020
) t
where next_ss is not null or next_ss <> ss + 1
union all
select 1, min(serial_start) - 1
from t
where year = 2020
group by year
having min(serial_start) <> 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.