我将如何在我的 CodeIgniter 模型中使用 ON DUPLICATE KEY UPDATE?

2021-11-20 00:00:00 model mysql codeigniter activerecord

我有一个 CodeIgniter/PHP 模型,我想向数据库中插入一些数据.

I have a CodeIgniter/PHP Model and I want to insert some data into the database.

但是,我在我的原始"SQL 查询中设置了这个:

However, I have this set in my 'raw' SQL query:

ON DUPLICATE KEY UPDATE duplicate=duplicate+1

我正在使用 CodeIgniter 并将我以前的所有控制器内 SQL 查询转换为 ActiveRecord.有没有办法从基于 ActiveRecord 的模型中做到这一点?

I am using CodeIgniter and am converting all my previous in-controller SQL queries to ActiveRecord. Is there any way to do this from within the ActiveRecord-based model?

谢谢!

杰克

推荐答案

无需修改任何核心文件即可添加ON DUPLICATE"语句.

You can add the "ON DUPLICATE" statement without modifying any core files.

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

我希望它会帮助某人.

相关文章