问题
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