mysqlは、selectステートメントのクエリ結果をinsertステートメントに挿入される値として使用します

実際の使用では、このようなシナリオが発生します。1つのテーブルの内容に基づいて、他のテーブルに関連データを追加する必要があります。今回はそれを使用します。

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

指示

1. selectステートメントを使用して、table1で必要なものを見つけます

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

2.挿入ステートメントの値を選択ステートメントに置き換えます 

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

3.挿入する必要のある値がtable1にない場合は、table2のフィールド順序に従ってselectステートメントに直接追加します。

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

 

おすすめ

転載: blog.csdn.net/wuchenlhy/article/details/110130747