MySql - 为其他表的每个ID插入行

Das*_*sto 11 mysql

我确信这很简单我只是无法获得正确的搜索条件来找到答案.但我有一张表用于ID的位置

ID| Name

1 | Foo

2 | Bar

3 | Blah
Run Code Online (Sandbox Code Playgroud)

和另一个表(table2),其中有一个引用这些位置ID的字段:

ID| LocationID | Foo | Bar

1 | 1          | ... | ...

2 | 2          | ..

3 | 5          | ...
Run Code Online (Sandbox Code Playgroud)

其中LocationId =来自位置的值.我想要做的是为位置中的每个ID(1,2,3 ......)添加一个记录到另一个表.例如:

"insert into table2 (locationID, foo, bar) values (1, "hello", "world");"

"insert into table2 (locationID, foo, bar) values (2, "hello", "world");"
Run Code Online (Sandbox Code Playgroud)

等等..

Pet*_*Wal 19

你可以使用INSERT INTO ... SELECT,所以为你的例子

INSERT INTO table2 (locationID, foo, bar) SELECT ID, 'hello', 'world' FROM table1
Run Code Online (Sandbox Code Playgroud)