在MySQL中插入几个具有一个查询的类似行

时间:2021-05-12 11:14:52

Consider a table with 2 fields:

考虑一个包含2个字段的表:

 tbl(Id int primary key,Name varchar(100))

Assume that this table contains one row with Id=3 and some unknown Name.

假设此表包含一行Id = 3和一些未知名称。

 Id   |   Name
 ---------------
 3    |   *****

I have an array of Ids, for example: array(4,6,7,10) How to put these Ids with the Name of row with Id=3 into this table with one query, So that the resulted table would be:

我有一个ID数组,例如:array(4,6,7,10)如何将带有Id = 3的行名称的这些ID放入此表中,并使用一个查询,这样得到的表将是:

 Id   |   Name
 ---------------
 3    |   *****
 ----------------
 4    |   *****
 ----------------
 6    |   *****
 ----------------
 7    |   *****
 ----------------
 10   |   *****

I can not use the Name's value in the query.

我不能在查询中使用Name的值。

I am thinking of a query like this:

我在考虑这样的查询:

insert into tbl(Id,Name) select (4,6,7,10),Name from tbl

1 个解决方案

#1


You need 2 queries, 1st get the name and 2nd do the insert with multi row insert

您需要2个查询,第1个获取名称,第2个执行插入多行插入

INSERT INTO Table ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )

#1


You need 2 queries, 1st get the name and 2nd do the insert with multi row insert

您需要2个查询,第1个获取名称,第2个执行插入多行插入

INSERT INTO Table ( Column1, Column2 ) VALUES
( Value1, Value2 ), ( Value1, Value2 )