如何从一张 MySQL 表格插入数据到另一个表格并设置一个列值?
我们首先来创建一张表格。以下为查询 −
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 表格中插入到另一张表格中并设置一个列值查询 −
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)
广告