tp批量添加
前提需要建立唯一索引,当命中唯一索引的时候会自动替换
1. Db->name('table')->insert($data,true);
直接使用 DB类下下面的 insertall($data,true);当第二参数为true的时候会自动替换,
但是fetchsql() 结果为 replace into 先执行delete 在执行update
2.使用model层的$midel->saveAll();
只需要在批量更新的数据中包含主键即可
$user = new User; $list = [ ['id'=>1, 'name'=>'thinkphp', 'email'=>'thinkphp@qq.com'], ['id'=>2, 'name'=>'onethink', 'email'=>'onethink@qq.com'] ]; $user->saveAll($list);
fetchsql() 结果为 on duplcate key update 先执行insert 然后在执行uodate 会保证原数据的完整性
奥利给

浙公网安备 33010602011771号