如何使用PHP mysqli在MySQL中增加值 [英] How to increment value in MySQL with PHP mysqli

查看:65
本文介绍了如何使用PHP mysqli在MySQL中增加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为col1的MySQL表中有一个整数列.现在,我需要将其值增加一些,例如1(也许是2、3或其他任何值).即,如果它已经拥有10的值,那么现在我希望它变为11.我知道,我可以通过以下方法进行操作:首先选择原始值,然后使用PHP对其进行递增,然后更新该值.但是我想知道是否有一种方法可以不必选择以前的值来增加它.

I have a integer column in a MySQL table called col1. Now, what I need is to increase its value by some number say 1 (maybe 2, 3 or anything). i.e. If it was already holding value of 10, now I want it to become 11. I know, I can do it by first selecting the original value, increment it with PHP and then update the value. But I wanted to know if there is a way through which I don't have to select the previous value to increment it.

推荐答案

这可以非常简单地完成,只需执行这样的查询

This can be done very simply, just execute a query like this

$sql = "UPDATE tablename SET col1=col1+1 WHERE key=99";

或您喜欢的任何值

$sql = "UPDATE tablename SET col1=col1+3 WHERE key=99";

这篇关于如何使用PHP mysqli在MySQL中增加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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