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)
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP