如何使用当前列值和codeigniter中的增量更新列值? [英] How to update a column value with the current column value plus an increment in codeigniter?

查看:68
本文介绍了如何使用当前列值和codeigniter中的增量更新列值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用当前列值和增量更新列值。我的表结构如下:

  id |菜单|优先级
__________________________

1 |一| 2
2 |二| 3
3 |三| 1

一个简单的查询我试过的是工作:

 更新表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')); 

但不起作用。

解决方案

根据文档


在执行
更新数据库时,您还可以使用$ this-> db-> set()函数。




  $ this-> db->其中('id',$ menu_ids [$ i]) 
- > set('menu_priority','menu_priority + 1',FALSE)
- > update($ this-> table ['menu']); set()还将接受可选的第三个参数($ escape)



< ,如果设置为FALSE,
将阻止数据转义。为了说明
的区别,这里是set()使用和不使用转义
参数。



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.

解决方案

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.

这篇关于如何使用当前列值和codeigniter中的增量更新列值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆