更新时间:2023-10-08 08:20:58
您可以按照以下操作:
$this->db->where('id', $post['identifier']);
$this->db->set('votes', 'votes+1', FALSE);
$this->db->update('users');
之所以有效,是因为第三个(可选)FALSE 参数告诉 CodeIgniter 不要用反引号 ('
) 保护生成的查询.这意味着生成的 SQL 将是:UPDATE users SET votes= votes + 1 WHERE id='44'
如果您注意到,从 '(votes+1)'
中删除了反引号,这会产生将 votes 属性增加 1 的预期效果.
The reason this works is because the third (optional) FALSE parameter tells CodeIgniter not to protect the generated query with backticks ('
). This means that the generated SQL will be:UPDATE users SET votes= votes + 1 WHERE id= '44'
If you notice, the backticks are removed from '(votes+1)'
, which produces the desired effect of incrementing the votes attribute by 1.