简体   繁体   English

如何使用 select 语句将子集表的数据插入超集表?

[英]How to insert the data of a subset table into a superset table using select statement?

I am trying to make a dynamic query where I want to insert data into the superset table from subset table.我正在尝试进行动态查询,我想将数据从子集表插入到超集表中。

Here are my tables这是我的桌子

Table A
|---------------------|------------------|
|          id         |  EmployeeName    |
|---------------------|------------------|
|           1         |       ABC1       |
|---------------------|------------------|
|           2         |       ABC2       |
|---------------------|------------------|
|           3         |       ABC3       |
|---------------------|------------------|

Table B
|----------------------|---------------------|------------------|
|          id          |       empid         |  EmployeeName    |    
|----------------------|---------------------|------------------|
|                      |                     |                  |
|----------------------|---------------------|------------------|
|                      |                     |                  |
|----------------------|---------------------|------------------|
|                      |                     |                  |
|----------------------|---------------------|------------------|

I want to insert data of table A into table B我想将表A的数据插入表B

here is the query I am trying这是我正在尝试的查询

insert into B (select * from A); 

It is not working and showing me that both tables have unequal number of columns.它不起作用并向我显示两个表的列数不相等。

By default an insert statement expects that all target columns are provided.默认情况下, insert语句期望提供所有目标列。 You can change that by enumerating the columns you want to insert in. This is a good practice that you should should stick to, since it makes the queries easier to read and maintain.您可以通过枚举要插入的列来更改它。这是您应该坚持的好习惯,因为它使查询更易于阅读和维护。

Presumably, you want:大概,你想要:

insert into b(id, employeeName)
select id, employeeName
from a

You need to map the columns so them can match.您需要 map 列,以便它们可以匹配。 See documentation 查看文档

Try this one:试试这个:

insert into B (id, EmployeeName) select id, EmployeeName from A; 

Insert into B(id, EmployeeName) values(select * from A); //This will work only If notNull constraint is not set on empid. //这仅适用于未在 empid 上设置 notNull 约束。

This is the most dynamic query that you can have for this situation.对于这种情况,这是您可以拥有的最动态的查询。 We had mentioned column names of table B because you are providing values of only 2 columns.我们提到了表 B 的列名,因为您只提供了 2 列的值。 If you dont want to use column name then you need thre remaining data in some other table and modify the table B a little bit.如果您不想使用列名,那么您需要在其他表中保留剩余数据并稍微修改表 B。

 Table A
|---------------------|------------------|
|          id         |  EmployeeName    |
|---------------------|------------------|
|           1         |       ABC1       |
|---------------------|------------------|
|           2         |       ABC2       |
|---------------------|------------------|
|           3         |       ABC3       |
|---------------------|------------------|

Table B
|----------------------|---------------------|------------------|
|          id          |       EmployeeName  |      empid       |    
|----------------------|---------------------|------------------|
|                      |                     |                  |
|----------------------|---------------------|------------------|
|                      |                     |                  |
|----------------------|---------------------|------------------|
|                      |                     |                  |
|----------------------|---------------------|------------------|
Table C
|---------------------|------------------|
|          id         |       empid      |
|---------------------|------------------|
|           1         |       1000       |
|---------------------|------------------|
|           2         |       1001       |
|---------------------|------------------|
|           3         |       1002       |
|---------------------|------------------|

Now you can run the query: insert into B values(select * from A inner join C on A.id=C.id);现在您可以运行查询: insert into B values(select * from A inner join C on A.id=C.id); this will populate your table A.这将填充您的表 A。

This is the final solution that worked for me because Id is an auto increment column and if you enter null into it, nothing changes.这是对我有用的最终解决方案,因为 Id 是一个自动增量列,如果您在其中输入 null ,则没有任何变化。

INSERT INTO B (SELECT NULL, A.* FROM A);插入 B(从 A 中选择 NULL,A.*);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM