MySQL 如何从一个表中插入到另一个表并设置一列的值
首先让我们创建一个表。以下是查询:
mysql> create table insertOneToAnotherTable
-> (
-> Value int
-> );
Query OK, 0 rows affected (0.60 sec)
以下是使用插入命令向表中插入一些记录的查询:
mysql> insert into insertOneToAnotherTable values(100);
Query OK, 1 row affected (0.08 sec)
mysql> insert into insertOneToAnotherTable values(200);
Query OK, 1 row affected (0.15 sec)
mysql> insert into insertOneToAnotherTable values(300);
Query OK, 1 row affected (0.13 sec)
mysql> insert into insertOneToAnotherTable values(400);
Query OK, 1 row affected (0.15 sec)
mysql> insert into insertOneToAnotherTable values(500);
Query OK, 1 row affected (0.12 sec)
mysql> insert into insertOneToAnotherTable values(600);
Query OK, 1 row affected (0.16 sec)
以下是使用select语句从表中显示所有记录的查询:
mysql> select * from insertOneToAnotherTable;
这将产生以下输出:
+-------+
| Value |
+-------+
| 100 |
| 200 |
| 300 |
| 400 |
| 500 |
| 600 |
+-------+
6 rows in set (0.00 sec)
以下是创建第二个表的查询:
mysql> create table recieveDateFromTable
-> (
-> Value1 int,
-> Value2 int
-> );
Query OK, 0 rows affected (0.83 sec)
以下是从一个MySQL表中插入到另一个表并设置一个列的值的查询:
mysql> insert into recieveDateFromTable(Value1,Value2) select Value,1000 from
insertOneToAnotherTable;
Query OK, 6 rows affected (0.14 sec)
Records: 6 Duplicates: 0 Warnings: 0
让我们从第二个表中显示所有记录。以下是查询:
mysql> select * from recieveDateFromTable;
这将产生以下输出:
+--------+--------+
| Value1 | Value2 |
+--------+--------+
| 100 | 1000 |
| 200 | 1000 |
| 300 | 1000 |
| 400 | 1000 |
| 500 | 1000 |
| 600 | 1000 |
+--------+--------+
6 rows in set (0.00 sec)
阅读更多:MySQL 教程