Shafizadeh
Shafizadeh

Reputation: 10380

How to use a sub-query as value for inserting?

I have a query like this:

INSERT INTO table1(col1,col2,col4)
            VALUES (1, (select col1 from table2 where col2 = :param), 1);

The above query works as well. Now I want to use two columns from table2, like this:

INSERT INTO table1(col1,col2,col3,col4)
            VALUES (1, (select col1,col2 from table2 where col2 = :param), 1);

But second query doesn't work, How can I fix it?

Upvotes: 1

Views: 26

Answers (1)

Mitch Wheat
Mitch Wheat

Reputation: 300837

INSERT INTO table1(col1, col2, col3, col4)
select 1, col1, col2, 1 
from table2 
where col2 = :param;

Upvotes: 3

Related Questions