在 MySQL 中,使用使用者定義變數增加資料庫欄位值,按指定百分比


首先,我們建立一個表 -

mysql> create table DemoTable
-> (
-> Amount int
-> );
Query OK, 0 rows affected (0.99 sec)

使用 insert 命令,在表中插入一些記錄 -

mysql> insert into DemoTable values(100);
Query OK, 1 row affected (0.23 sec)

mysql> insert into DemoTable values(200);
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable values(500);
Query OK, 1 row affected (0.13 sec)

使用 select 語句從表中顯示所有記錄 -

mysql> select *from DemoTable;

輸出

將生成以下輸出 -

+--------+
| Amount |
+--------+
| 100    |
| 200    |
| 500    |
+--------+
3 rows in set (0.00 sec)

以下是增加資料庫欄位值,按指定百分比的查詢 -

mysql> set @rate=10;
Query OK, 0 rows affected (0.00 sec)

mysql> update DemoTable
-> set Amount=Amount*(1+@rate/100);
Query OK, 3 rows affected (0.18 sec)
Rows matched: 3 Changed: 3 Warnings: 0

讓我們再次查看錶格記錄 -

mysql> select *from DemoTable;

輸出

將生成以下輸出 -

+--------+
| Amount |
+--------+
| 110    |
| 220    |
| 550    |
+--------+
3 rows in set (0.00 sec)

更新於:30-6 月-2020

324 次瀏覽

開啟你的職業生涯

完成課程即可獲得認證

開始吧
廣告