2 回答

TA貢獻2051條經(jīng)驗 獲得超10個贊
您是否有一些獨特的數(shù)據(jù)點將記錄連接在一起,例如時間戳或數(shù)據(jù)來源的 ID?如果是這樣,您可以使用updateOrInsert
DB::table($tableName)
? ? ->updateOrInsert(
? ? ? ? ['remote_id' => $insert_array['remote_id'],
? ? ? ? [
? ? ? ? ? ?'example_field_1' => $insert_array['example_field_1'],
? ? ? ? ? ?'example_field_2' => $insert_array['example_field_2'],
? ? ? ? ]
? ? );

TA貢獻1789條經(jīng)驗 獲得超8個贊
以下是解決該問題的一些示例代碼:
$values = array();
//set up values for binding to prevent SQL injection
foreach ($insert_array as $item) {
$values[] = '?';
}
//array values need to be in a "flat array"
$flat_values = implode(", ", $values);
//add in separators ` to column names
$columns = implode("`, `",array_keys($insert_array));
//write sql statement
$sql = "INSERT IGNORE INTO `example_table` (`$columns`) VALUES
($flat_values) ON DUPLICATE KEY UPDATE id = '$id'";
DB::insert($sql, array_values($insert_array));
}
- 2 回答
- 0 關(guān)注
- 189 瀏覽
添加回答
舉報