以下是执行加号(+)运算符的语法-
update yourTableName set yourColumnName3=(yourColumnName1+yourColumnName2)
上面的语法仅适用于加号运算符。您需要将-,*,/等符号更改为其他操作。让我们首先创建一个表-
create table DemoTable -> ( -> Number1 int, -> Number2 int, -> AddResult int, -> MinusResult int, -> MultiplyResult int, -> DivideResult int -> );
使用插入命令在表中插入一些记录-
insert into DemoTable(Number1,Number2) values(40,20);
使用select语句显示表中的所有记录-
select *from DemoTable;
这将产生以下输出-
+---------+---------+-----------+-------------+----------------+--------------+ | Number1 | Number2 | AddResult | MinusResult | MultiplyResult | DivideResult | +---------+---------+-----------+-------------+----------------+--------------+ | 40 | 20 | NULL | NULL | NULL | NULL | +---------+---------+-----------+-------------+----------------+--------------+ 1 row in set (0.00 sec)
以下是在MySQL中使用UPDATE时执行加,减,乘和除等运算的查询-
update DemoTable set AddResult=(Number1+Number2); Rows matched: 1 Changed: 1 Warnings: 0 update DemoTable set MinusResult=(Number1-Number2); Rows matched: 1 Changed: 1 Warnings: 0 update DemoTable set MultiplyResult=(Number1*Number2); Rows matched: 1 Changed: 1 Warnings: 0 update DemoTable set DivideResult=(Number1/Number2); Rows matched: 1 Changed: 1 Warnings: 0
让我们再次检查表记录-
select *from DemoTable;
这将产生以下输出-
+---------+---------+-----------+-------------+----------------+--------------+ | Number1 | Number2 | AddResult | MinusResult | MultiplyResult | DivideResult | +---------+---------+-----------+-------------+----------------+--------------+ | 40 | 20 | 60 | 20 | 800 | 2 | +---------+---------+-----------+-------------+----------------+--------------+ 1 row in set (0.00 sec)