使用MySQL中的用户定义变量将数据库字段值增加指定的百分比

让我们首先创建一个表-

mysql> create table DemoTable
-> (
-> Amount int
-> );

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable values(100);

mysql> insert into DemoTable values(200);

mysql> insert into DemoTable values(500);

使用select语句显示表中的所有记录-

mysql> select *from DemoTable;

输出结果

这将产生以下输出-

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

以下是将数据库字段值增加指定百分比的查询-

mysql> set @rate=10;

mysql> update DemoTable
-> set Amount=Amount*(1+@rate/100);
Rows matched: 3 Changed: 3 Warnings: 0

让我们再次检查表记录-

mysql> select *from DemoTable;

输出结果

这将产生以下输出-

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