主要问题是假设我现在有一个值存储在数据库 = 26 中,如果用户执行某些操作并且我必须将值增加 1,sql 中是否有任何预定义的方式来执行此操作。
现在我正在提取值然后添加 1 然后再次更新条目。
是的,运行如下 SQL:
update yourTableName
set theColumnYouWant=theColumnYouWant+1
where yourConditions=YourConditionCriteria
更新语法的具体示例:
mysql> select * from first;
+------+-------+
| bob | title |
+------+-------+
| 1 | aaaa |
| 2 | bbbb |
| 3 | cccc |
| 4 | NULL |
| 5 | eeee |
+------+-------+
5 rows in set (0.00 sec)
mysql> update first set bob=bob+1 where title='eeee';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from first;
+------+-------+
| bob | title |
+------+-------+
| 1 | aaaa |
| 2 | bbbb |
| 3 | cccc |
| 4 | NULL |
| 6 | eeee |
+------+-------+
5 rows in set (0.00 sec)