在MySQL中插入时如何将正值转换为负值?
让我们先创建一个表
mysql> create table recordsDemo -> ( -> UserId int, -> Value int -> );
现在,使用insert命令在表中插入一些记录。
查询如下
mysql> insert into recordsDemo values(1,10); mysql> insert into recordsDemo values(3,598); mysql> insert into recordsDemo values(5,786); mysql> insert into recordsDemo values(7,189); mysql> insert into recordsDemo values(9,345);
使用select语句显示表中的所有记录。
查询如下
mysql> select *from recordsDemo;
以下是输出
+--------+-------+ | UserId | Value | +--------+-------+ | 1 | 10 | | 3 | 598 | | 5 | 786 | | 7 | 189 | | 9 | 345 | +--------+-------+ 5 rows in set (0.00 sec)
创建第二个表。创建第二个表的查询如下
mysql> create table PositiveToNegativeValueDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Money int -> );
这是在插入时将正值转换为负值的查询
mysql> insert into PositiveToNegativeValueDemo(Id,Money) -> select UserId,(-1*Value) from recordsDemo; Records: 5 Duplicates: 0 Warnings: 0
现在,使用select语句从表中检查表记录。
查询如下
mysql> select *from PositiveToNegativeValueDemo;
以下是输出
+------+-------+ | Id | Money | +------+-------+ | 1 | -10 | | 3 | -598 | | 5 | -786 | | 7 | -189 | | 9 | -345 | +------+-------+ 5 rows in set (0.00 sec)
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短