Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert multiple rows from select into another table

let's say i have `table1(col1,col2,col3), the values to insert on col1 and col3 will be the same, but the values to insert on col2 come from the result of a select query.

How can i write my query so i can do this multiple insert at once ? Example of what the query should do :

col1  |  col2   | col3

 1        val1     0
 1        val2     0
 1        val3     0
like image 425
RidRoid Avatar asked Jun 26 '15 11:06

RidRoid


1 Answers

If I understand correctly, you would use insert . . .select:

insert into table1(col1, col2, col3)
    select 1, col2, 0
    from <your other query here>;
like image 126
Gordon Linoff Avatar answered Oct 21 '22 05:10

Gordon Linoff