MySQL如何插入一个SELECT子查询返回多行的表?

MySQL如何插入一个SELECT子查询返回多行的表?

INSERT INTO Results ( People, names, ) VALUES ( ( SELECT d.id FROM Names f JOIN People d ON d.id = f.id ), ( "Henry" ), ); 

我想填充新的表格,从这个子查询返回的所有结果。 如何做到这一点,而不会得到错误1242(21000):子查询返回多个行

 INSERT INTO Results (People, names ) SELECT d.id, 'Henry' FROM Names f JOIN People d ON d.id = f.id 

将静态stringHenry与您的SELECT查询结合起来。

 INSERT INTO Results ( People, names, ) VALUES ( ( SELECT d.id FROM Names f JOIN People d ON (d.id = f.id) limit 1 ), ( "Henry" ), ); 
  INSERT INTO Results ( People, names, ) SELECT d.id, 'Henry' FROM Names f JOIN People d ON d.id = f.id 

这是我发现的效果很好。 这是一个有点长,但很多额外的数据需要洗牌。

从table2中插入多行到表1中的值。 例子:

 INSERT INTO table1 (col1, col2, col3, col4, col5) SELECT col1,col2,col3,col4,col5 FROM table2 t2 WHERE t2.val2 IN (MULTIPLE VALUES) AND (Another Conditional); 

您可以插入硬编码的值来插入多行重复数据:

 INSERT INTO table1 (col1, col2, col3, col4, col5) SELECT "Value", col2, col3, "1900-01-01","9999-12-31" FROM table2 t2 WHERE t2.val2 IN (MULTIPLE VALUES) AND (Another Conditional); 

请注意:“Value”,“1900-01-01”,“9999-12-31”将在插入的所有行上重复。

插入ec_element(parentid,name)selectelementid,从ec_element'STARTUP'where name ='BG';

插入语句从满足条件的表中获取值为elementid,并使用标签string。