PHP 如何查询两个数字之间的缺失值?

你好,我有一个像这样命名数字的 mysql 表;

ID 数量
1 3002
2 3004
2000 7545

当我需要插入一个新的数据产品表时,我必须找到第一个不在该表中的数字(3000 到 35000)。我的意思是我需要在不在数字表上的这些数字之间找到第一个数字。我怎样才能找到它?

$statement = $con->prepare("SELECT * FROM numbers");
$statement->execute();

$result = $statement->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $row)

{
    $arr1[] = $row['numbers'];

}
$arr2 = range(3000,35000);                                                    
$missing_numbers = array_diff($arr2,$arr1);
print_r($missing_numbers);

我尝试了 array_diff,但我给出了不同键的结果。当我写 $missing_numbers[0] 时,我想看到第一个丢失的数字。

catheone 回答:PHP 如何查询两个数字之间的缺失值?

如果您只想要第一个缺失的数字,按编号顺序读取行并检查第一个不在您所追求的序列中的行可能会更容易。所以一个查询(以起点和终点作为参数)按数字和计数器排序,这是它期望在每一行上得到的......

$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)

输出将根据需要选择所有数字。 enter image description here

本文链接:https://www.f2er.com/1231.html

大家都在问