MySQL 如何在插入MySQL时将正数转为负数

MySQL 如何在插入MySQL时将正数转为负数

首先,让我们创建一个表:

mysql> create table recordsDemo
   -> (
   -> UserId int,
   -> Value int
   -> );
Query OK, 0 rows affected (0.52 sec)
Mysql

现在使用插入命令将一些记录插入表中。

查询如下:

mysql> select *from recordsDemo;
Mysql
mysql> insert into recordsDemo values(1,10);
Query OK, 1 row affected (0.17 sec)
mysql> insert into recordsDemo values(3,598);
Query OK, 1 row affected (0.18 sec)
mysql> insert into recordsDemo values(5,786);
Query OK, 1 row affected (0.25 sec)
mysql> insert into recordsDemo values(7,189);
Query OK, 1 row affected (0.16 sec)
mysql> insert into recordsDemo values(9,345);
Query OK, 1 row affected (0.14 sec)
Mysql

使用select语句从表中显示所有记录。

查询如下:

mysql> select *from recordsDemo;
Mysql

如下是输出结果:

+--------+-------+
| UserId | Value |
+--------+-------+
| 1      | 10    |
| 3      | 598   |
| 5      | 786   |
| 7      | 189   |
| 9      | 345   |
+--------+-------+
5 rows in set (0.00 sec)
Mysql

创建第二个表。创建第二个表的查询如下:

mysql> create table PositiveToNegativeValueDemo
   -> (
   -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> Money int
   -> );
Query OK, 0 rows affected (0.83 sec)
Mysql

以下是将正数转为负数并插入的查询:

mysql> insert into PositiveToNegativeValueDemo(Id,Money)
   -> select UserId,(-1*Value) from recordsDemo;
Mysql
Query OK, 5 rows affected (0.15 sec)
Records: 5 Duplicates: 0 Warnings: 0
Mysql

现在使用select语句从表中检查记录。

查询如下:

mysql> select *from PositiveToNegativeValueDemo;
Mysql

如下是输出结果:

+------+-------+
| Id   | Money |
+------+-------+
| 1    | -10   |
| 3    | -598  |
| 5    | -786  |
| 7    | -189  |
| 9    | -345  |
+------+-------+
5 rows in set (0.00 sec)
Mysql

阅读更多:MySQL 教程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册