Search code examples
mysqlsqlvariablesmultiple-insert

Multiple INSERT INTO statements within one query


I have two tables: BT and ST. Here is what I hope I can accomplish:

INSERT INTO BT (c1,c2,c3) values (v1,v2,v3)

ST is a table with 89 rows and one column. Can and if so, how can I formulate a query so that the insert works for every one of the 89 values from the ST table?

(Bear in mind: the values are random and hold no mathematical connection by which I could tie them in)

c1-c3= column1,column2,column3

v1-v3= value1,value2,value3 (v2 and v3 are predefined and set and do not require alteration)

I tried the more obvious things without success, such as:

INSERT INTO BT (c1,c2,c3) values ((select c1 from ST),v2,v3)

But all that line does is return a general sql syntax error.

I acknowledge this can be performed with 89 rows of "INSERT INTO" and me changing that one value every time, but can this be done in a more elegant way?

I'm using MySQL


Solution

  • You just want insert . . . select:

    insert into BT (c1, c2, c3) 
       select c1, v2, v3
       from ST