MySQL查询从与常量合并的另一个表中插入数据?

让我们首先创建一个表-

mysql> create table DemoTable1(Name varchar(100));

使用插入命令在表中插入一些记录-

mysql> insert into DemoTable1 values('John');
mysql> insert into DemoTable1 values('Chris');
mysql> insert into DemoTable1 values('Robert');

使用select语句显示表中的所有记录-

mysql> select *from DemoTable1;

这将产生以下输出-

+--------+
| Name   |
+--------+
| John   |
| Chris  |
| Robert |
+--------+
3 rows in set (0.00 sec)

以下是创建第二个表的查询-

mysql> create table DemoTable2(
   ConstantValue int,
   EmployeeName varchar(100)
);

以下是从另一个表中插入与常量合并的数据的查询-

mysql> insert into DemoTable2(ConstantValue,EmployeeName) select '1000',Name from DemoTable1;
Records: 3 Duplicates: 0 Warnings: 0

使用select语句显示表中的所有记录-

mysql> select *from DemoTable2;

这将产生以下输出-

+---------------+--------------+
| ConstantValue | EmployeeName |
+---------------+--------------+
| 1000          | John         |
| 1000          | Chris        |
| 1000          | Robert       |
+---------------+--------------+
3 rows in set (0.00 sec)