MySQL将数据库字段增加1?

您可以使用update命令增加数据库。语法如下-

UPDATE yourTableName
set yourColumnName=yourColumnName+1
where condition;

为了理解上述语法,让我们首先创建一个表。创建表的查询如下-

mysql> create table IncrementBy1
   -> (
   -> Id int,
   -> Name varchar(100),
   -> CounterLogin int
   -> );

使用插入命令插入一些记录。在表中插入记录的查询如下-

mysql> insert into IncrementBy1 values(100,'John',30);

mysql> insert into IncrementBy1 values(101,'Carol',50);

mysql> insert into IncrementBy1 values(102,'Bob',89);

mysql> insert into IncrementBy1 values(103,'Mike',99);

mysql> insert into IncrementBy1 values(104,'Sam',199);

mysql> insert into IncrementBy1 values(105,'Tom',999);

使用select语句显示表中的所有记录。查询如下-

mysql> select *from IncrementBy1;

输出结果

+------+-------+--------------+
| Id   | Name  | CounterLogin |
+------+-------+--------------+
|  100 | John  |           30 |
|  101 | Carol |           50 |
|  102 | Bob   |           89 |
|  103 | Mike  |           99 |
|  104 | Sam   |          199 |
|  105 | Tom   |          999 |
+------+-------+--------------+
6 rows in set (0.00 sec)

这是使数据库字段增加1的查询-

mysql> update IncrementBy1
   -> set CounterLogin=CounterLogin+1
   -> where Id=105;
Rows matched: 1 Changed: 1 Warnings: 0

现在,您可以检查特定记录是否递增。值999已经增加了1,因为我们在上面所示的Id = 105处增加了值。

以下是用于检查记录的查询-

mysql> select *from IncrementBy1 where Id=105;

输出结果

+------+------+--------------+
| Id   | Name | CounterLogin |
+------+------+--------------+
|  105 | Tom  | 1        000 |
+------+------+--------------+
1 row in set (0.00 sec)