如何将一个 MySQL 表的 INSERT INTO 插入到另一个表并设置一列的值?
mysqlmysqli database
让我们首先创建一个表。以下是查询 −
mysql> create table insertOneToAnotherTable -> ( -> Value int -> ); Query OK, 0 rows affected (0.60 sec)
下面是使用 insert 命令在表中插入一些记录的查询 −
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 表 INSERT INTO 到另一个表并设置一列的值 − 的查询
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)