嗨,我有一个接收数组的函数,对于数组中的每个元素,它运行一个选择随机结果 id 的查询,但是我想避免重复如何检查结果 id 是否重复并再次运行查询?一直在循环中而不是跳过元素?这是我的尝试(注意:$array_result 是 7 个元素),也选择 distinct 不起作用,因为我必须单独为每个数组元素运行查询
$queryn = "select taxonomic_units.tsn, hierarchy.hierarchy_string, hierarchy.TSN, taxonomic_units.rank_id from hierarchy left join taxonomic_units on hierarchy.TSN = taxonomic_units.tsn where taxonomic_units.rank_id = 220 and hierarchy.hierarchy_string LIKE '%$array_result[0]%'order by rand() limit 1";
$resultn = $dbn -> Execute($queryn);
$rown=$resultn->FetchRow();
$newspecies = $rown['tsn'];
$result_array[] = $newspecies;
for($i=1; $i<count($array_result);$i++){
$previous = implode(',', $result_array);
$queryn = "select taxonomic_units.tsn,
hierarchy.hierarchy_string,
hierarchy.TSN,
taxonomic_units.rank_id
from hierarchy
left join taxonomic_units
on hierarchy.TSN = taxonomic_units.tsn
where taxonomic_units.rank_id = 220
and hierarchy.hierarchy_string LIKE '%$array_result[$i]%'
and taxonomic_units.tsn not in ('$previous')
order by rand()
limit 1";
$resultn = $dbn -> Execute($queryn);
$rown=$resultn->FetchRow();
$newspecies = $rown['tsn'];
$result_array[] = $newspecies;
}