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

mysqlmysqli database

首先我们创建一个表 −

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)

相关文章