How to update a column value with the current column value plus an increment in codeigniter?

偶尔善良 提交于 2019-12-24 12:36:56

问题


I want to to update a column value with the current column value plus an increment. My table structure is as follows:

id   |   menu   | priority
__________________________

 1   |   One    |   2
 2   |   Two    |   3
 3   |   Three  |   1

A simple query I have tried that is working:

update table set priority=priority+1 where id=2

in codeigniter:

$update_new_pri=$this->db->where('id',$menu_ids[$i])->update($this->table['menu'],array('menu_priority'=>'menu_priority+1'));

But it's not working.


回答1:


According to docs

You may also use the $this->db->set() function when performing updates to the database.

$this->db->where('id',$menu_ids[$i])
     ->set('menu_priority', 'menu_priority+1', FALSE)      
     ->update($this->table['menu']);

set() will also accept an optional third parameter ($escape), that will prevent data from being escaped if set to FALSE. To illustrate the difference, here is set() used both with and without the escape parameter.



来源:https://stackoverflow.com/questions/23664910/how-to-update-a-column-value-with-the-current-column-value-plus-an-increment-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!