如果您只想要第一个缺失的数字,按编号顺序读取行并检查第一个不在您所追求的序列中的行可能会更容易。所以一个查询(以起点和终点作为参数)按数字和计数器排序,这是它期望在每一行上得到的......
$startPoint = 3000;
$endPoint = 35000;
$statement = $db->prepare("SELECT number
FROM numbers
WHERE number >= :start
and number <= :end
order by number");
$statement->execute([
'start' => $startPoint,'end' => $endPoint
]);
$expected = $startPoint;
while ($row = $statement->fetch()) {
if ( $row['number'] != $expected ) {
echo "Missing=" . $expected;
break;
}
$expected++;
}
,
我认为您的解决方案将起作用,唯一缺少的是可以通过使用 array_values() 函数完成的索引的重新排序。将您的最后几行代码更新为 -
$missing_numbers = array_values(array_diff($arr2,$arr1));
print_r($missing_numbers);
因此在打印 $missing_numbers[0] 时,您应该得到第一个缺失的数字。
,
简单,只需要在array_diff之后重置键:
...
$missing_numbers = array_values(missing_numbers);
print_r($missing_numbers);
,
如果您只想通过使用 MySQL 查询来执行此操作,然后在 PHP 中获得结果,您需要执行以下操作:
用你的例子创建临时表:
create table test123(id integer,Number varchar(100))
#insert into test123 (ID,Number) values (1,'3002');
#insert into test123 (ID,Number) values (2,'3004');
#insert into test123 (ID,Number) values (2000,'7545');
创建范围为 3000 - 35000 的临时表
CREATE TEMPORARY TABLE IF NOT EXISTS tableTest AS (
SELECT @row := @row + 1 AS row FROM
(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t,(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t2,(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t3,(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t4,(select 0 union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) t5,(SELECT @row:=3000) numbers WHERE @row < 35000
)
最后,您可以选择不在您的表格中的数字和数据
select * from tableTest t where t.row not in ( select Number from test123)
输出将根据需要选择所有数字。
本文链接:https://www.f2er.com/1231.html