检查10个新行是否唯一,然后添加到表,记录布尔值


Check if set of 10 new rows is unique before adding to table, record with boolean

所以每天,我收集前10名列表,创建一个临时表temp。有一个大表timeline,它聚合了这些列表,包含以下列:

date, num, id, changed 

我想检查10本书的temp中的这个集合是否唯一,或者它的源是否没有更新并且值是重复的。我想在添加到时间轴之前,在temp中捕获这个布尔值changed

我相信有更好的方法来做到这一点,但在PHP中,我已经创建了一个数组的前一天和最后一集添加,并与当前集比较,存储结果在一个单独的数组$change_array。我不知道如何将其添加回temp,即将此0和1数组转换为一列,或将值添加到一个命令中的各自行。仅供参考,这些命令看起来像这样:

$last_ten = "SELECT num, asin FROM timeline ORDER BY date DESC, num ASC LIMIT 0,10";
$cur_ten = "SELECT num, asin FROM temp ORDER BY num ASC";
...
for ($ind = 1; $ind <= 10; $ind++) {
    $change_array[$ind] = ($currents[$ind] == $prevs[$ind]) ? 0 : 1;
}

谢谢你的帮助!

决定简化,按照建议使用array_intersect,如果整个集合相差哪怕一行,那么该日期的所有行都被标记为不同:

$last_ten = "SELECT num, asin FROM timeline t ORDER BY t.date DESC, t.num ASC LIMIT 0,10";
$cur_ten = "SELECT num, asin FROM $table ORDER BY num ASC";
$prevs = array(1 => "", 2 => "", 3 => "", 4 => "", 5 => "", 6 => "", 7 => "", 8 => "", 9 => "",  10 => "");
    while($r = mysql_fetch_array($last_ten_res))
    {
        $num = $r['num'];
        $prevs[$num] = $r['asin'];
    }
$currents = array(1 => "", 2 => "", 3 => "", 4 => "", 5 => "", 6 => "", 7 => "", 8 => "", 9 => "",  10 => "");
    while($r = mysql_fetch_array($cur_ten_res))
    {
        $num = $r['num'];
        $currents[$num] = $r['asin'];
    }
$different = count(array_intersect($prevs, $currents)) == 10 ? 0 : 1;
$add_change = "ALTER TABLE $table ADD changed INT(1) DEFAULT $different;";