简体   繁体   English

MySQL INSERT多行,具体取决于选择其他表的结果

[英]MySQL INSERT multiple rows depending on result from SELECTing other table

The following query gives me a result of different IDs from the table one depending on condition : 以下查询根据condition为我提供了与表one不同的ID的结果:

SELECT id FROM one WHERE condition

I need to use these ids to insert data into another table: 我需要使用这些ID将数据插入到另一个表中:

INSERT INTO two (id, col1, col2) VALUES
(1st-id-from-above, 'foo','bar'),
(2nd-id-from-above, 'foo','bar'),
…
(last-id-from-above, 'foo','bar');

'foo' and 'bar' is always the same, they're static. 'foo''bar'始终相同,它们是静态的。 The only dynamic things are: 唯一动态的事情是:

  • the number of IDs (ie the number of rows to insert into two ), ID的数量(即要插入two行的行数),
  • the IDs itself (ie the values to insert into the first column of two ). ID本身(即插入two值的第一列的值)。

How can I do the job automatically? 我如何自动完成这项工作?

The two tables are not identical, so I'm not dealing with moving rows from one table to another. 这两个表不完全相同,因此我不处理将行从一个表移动到另一个表的问题。

INSERT INTO two (id, col1, col2)
SELECT id, 'foo', 'bar'
FROM one 
WHERE condition

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

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