mysql uses the query result of the select statement as the value to be inserted in the insert statement

In actual use, you will encounter such a scenario. You need to add associated data in other tables based on the contents of one table. This time it will be used

insert into {table_name} select x1,x2,x3... from {table_name2} where xxx

Instructions

1. Find what you need in table1 through the select statement

select id, name, ... from table1 where id in (1, 2, 3, ...);

2. Replace the values ​​in the insert statement with the select statement 

insert into table2 select id,name... from table1 where xxx

3. If the value to be inserted is not in table1, just add it directly in the select statement according to the field order of table2.

//其中 100 是table2 表中的第三个字段的值
//'张三' 是table2 表中的第四个字段的值
insert into table2 select id,name,100,'张三'... from table1 where xxx

 

Guess you like

Origin blog.csdn.net/wuchenlhy/article/details/110130747