[英]How to find missing data either in array or in mySQL table?
我有一个装满值(推特ID)的数组,我想在最低ID和最高ID之间找到丢失的数据吗? 是否愿意分享有关如何执行此操作的简单功能或想法?
另外,我想知道我是否可以对mySQL做同样的事情? 我已经索引了键。 该表现在包含25万行,因此临时表然后进行联接将不会非常快速或有效。 我可以做一个PHP循环来遍历数据,但这也将花费很长时间,并且会占用大量内存。 我可以运行特定的mysql查询吗? 或者我可以以某种方式从上面使用该功能吗?
谢谢James Hartig http://twittertrend.net
我有类似的要求,并编写了一个函数,该函数将返回缺少的ID的列表。
---------------------------
create function dbo.FreeIDs ()
---------------------------
returns @tbl table (FreeID int)
as
begin
declare @Max int
declare @i int
select @Max = MAX(ID) from [TheTable]
set @i = 0
while @i < @Max begin
set @i = @i + 1
if not exists (select * from [TheTable] where ID = @i)
insert into @tbl select @i
end
return
end
您是指顺序编号吗?
在这种情况下
$new_ids = range($lowid, $highid, 1);
$ids = array_merge($ids, $new_ids);
$ids = array_unique($ids);
sort($ids);
在SQL中(带占位符)
SELECT key, other_data from `table` WHERE key > :low_id AND key < :high_id
您的range()给了我一个好主意,您的代码无法正常工作,因为唯一保留了唯一键,因此我只剩下范围函数的结果。
但是,这可行:
$diff = array_values(array_diff(range(min($array), max($array), 1), $array)); //returns array of incomplete values
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.