如果在keyigniter中有重复的密钥更新

如果在keyigniter中有重复的密钥更新

本文介绍了INSERT批处理,如果在keyigniter中有重复的密钥更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以执行批量插入查询,如果键已经存在,请在codeigniter中更新该行?我浏览了文档,仅发现了insert_batch和update_batch.但是,如何使用活动记录中的重复键更新行?如果在batch_insert中没有插入或更新一行,会发生什么情况?所有插入失败还是仅插入那一行?

Is there any way of performing in batch Insert query and if the key already exists, UPDATE that row in codeigniter?I have gone through the documentation and found only insert_batch and update_batch. But how to update the row with duplicate key in active records? And what happens if one row fails to be inserted or updated in batch_insert? All insertion fails or only that row?

推荐答案

您将不得不通过添加"ON DUPLICATE"语句来进行很少的自定义查询

You will have to go with little custom query by adding "ON DUPLICATE" statement

$sql = $this->db->insert_string('YourTable', $data) . ' ON DUPLICATE KEY UPDATE duplicate=duplicate+1';
$this->db->query($sql);
$id = $this->db->insert_id();

还请查看此信息,它将为您提供更好的解决方案

Also please check this out, it will give you better solution

这篇关于INSERT批处理,如果在keyigniter中有重复的密钥更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:26