从两个不同的表插入一列到表的2列

时间:2022-06-27 08:06:09

This is may sample database

这可能是示例数据库

TABLE A-----> TABLE B <-----TABLE C

The arrows show the relationship

箭头显示了这种关系

TABLE A 
{ 
   tblA_col1 <----primary key
} 
TABLE A
 (tblA_col1)
  | A_1  |<----data


TABLE C 
{ 
  tblC_col1 <----primary key
}
TABLE C
 (tblC_col1)
  | C_1  |<----data
  | C_2  |
  | C_3  |


TABLE B
{
  tblB_col1 <----primary key
  tblA_col1 <----col from table A
  tblC_col1 <----col from table C
}
TABLE B
(tblB_col1 |  tblA_col1  |  tblC_col1)
|   B_1    |     A_1     |    C_1    |
|   B_2    |     A_1     |    C_2    |
|   B_3    |     A_1     |    C_3    |   

Now my problems is how to insert the data in TABLE B with just one query?

现在我的问题是如何只用一个查询插入表B中的数据?

2 个解决方案

#1


insert into B (tblA_col1, tblC_col1 )
select A.tblA_col1, C.tblC_col1 from 
A, C; 

Is this what you wanted?

这是你想要的吗?

#2


Please refer to this thread as the answer is related with your problem.

请参阅此主题,因为答案与您的问题有关。

MySQL doesn't support multi-table insertion in a single INSERT statement

MySQL不支持在单个INSERT语句中插入多表

However, you CAN use a transaction and have both of them be contained within one transaction

但是,您可以使用事务并将它们都包含在一个事务中

sql - insert into multiple tables in one query

sql - 在一个查询中插入多个表

#1


insert into B (tblA_col1, tblC_col1 )
select A.tblA_col1, C.tblC_col1 from 
A, C; 

Is this what you wanted?

这是你想要的吗?

#2


Please refer to this thread as the answer is related with your problem.

请参阅此主题,因为答案与您的问题有关。

MySQL doesn't support multi-table insertion in a single INSERT statement

MySQL不支持在单个INSERT语句中插入多表

However, you CAN use a transaction and have both of them be contained within one transaction

但是,您可以使用事务并将它们都包含在一个事务中

sql - insert into multiple tables in one query

sql - 在一个查询中插入多个表