[英]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: 唯一动态的事情是:
two
), ID的数量(即要插入two
行的行数), 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.