[英]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.