0

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

date, num, id, changed 

我想检查这 10 本书的临时集合是否是唯一的,或者它的来源是否尚未更新并且值是否重复。在添加到时间线之前,我想在temp中以 boolean, 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;
}

谢谢你的帮助!

4

1 回答 1

0

决定只是为了简化,按照建议使用 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;";
于 2013-01-14T05:40:45.093 回答