Home  >  Article  >  php教程  >  进行数据字段加减操作

进行数据字段加减操作

WBOY
WBOYOriginal
2016-06-07 11:45:422042browse

经常有需要对某个数据表的计数字段进行加减操作,我们来看下在ThinkPHP中的具体使用办法。
最简单的,使用下面方法对score自动加1:M('User')->where('id=5')->setInc('score');当然,也可以加更多的积分:M('User')->where('id=5')->setInc('score',5);当然也可以减1操作M('User')->where('id=5')->setDec('score');setInc和setDec方法只能单独对一个字段进行操作,如果你的字段加减操作要和其他字段的更新一起的话,则需要采用表达式更新的方式了,例如:$User = M('User');<br> $User->id = 5;<br> $User->nickname = 'ThinkPHP';<br> $User->score = array('exp','score+5');<br> $User->save();表示对id为5的用户数据进行昵称和积分修改操作。$User->score = array('exp','score+5');这段代码就称之为表达式更新。

AD:真正免费,域名+虚机+企业邮箱=0元

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn