MySQL 將資料庫欄位增大 1?


你可以使用更新命令對資料庫進行增量。語法如下 -

UPDATE yourTableName
set yourColumnName=yourColumnName+1
where condition;

為了瞭解上述語句,我們先建立一個表格。建立表格的查詢如下 -

mysql> create table IncrementBy1
   -> (
   -> Id int,
   -> Name varchar(100),
   -> CounterLogin int
   -> );
Query OK, 0 rows affected (0.63 sec)

使用插入命令插入一些記錄。在表格中插入記錄的查詢如下 -

mysql> insert into IncrementBy1 values(100,'John',30);
Query OK, 1 row affected (0.17 sec)

mysql> insert into IncrementBy1 values(101,'Carol',50);
Query OK, 1 row affected (0.15 sec)

mysql> insert into IncrementBy1 values(102,'Bob',89);
Query OK, 1 row affected (0.25 sec)

mysql> insert into IncrementBy1 values(103,'Mike',99);
Query OK, 1 row affected (0.18 sec)

mysql> insert into IncrementBy1 values(104,'Sam',199);
Query OK, 1 row affected (0.36 sec)

mysql> insert into IncrementBy1 values(105,'Tom',999);
Query OK, 1 row affected (0.18 sec)

使用選擇語句顯示錶格中的所有記錄。查詢如下 -

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;
Query OK, 1 row affected (0.45 sec)
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)

更新於:30-7-2019

4K+ 次瀏覽

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.