Reputation: 2657
I need to do the following:
INSERT INTO table1 (field1,field2,field3,field4,field5) VALUES ("customvalue1",SELECT field2,field3,field4,field5 FROM table2 WHERE condition=true)
Is there a way to do it? what would be the best way?
Upvotes: 0
Views: 169
Reputation: 1024
i think it is helpful for you
INSERT INTO firsttable (field1,field2,field3,field4,field5)
SELECT "customvalue1", field2, field3, field4, field5
FROM secondtable
WHERE condition;
Upvotes: 1
Reputation: 32001
This is called insert data from one table to other table based on condition
create table table1
(id int,
name varchar(100)
)
;
create table table2
(id int,
name varchar(100),
salary double(10,2)
)
;
insert into table1 values(1,'A'),(2,'B'),(3,'C');
insert into table2(id,name,salary)
select id,name,6000 from table1 where table1.name='A'
http://sqlfiddle.com/#!9/8b30fb/1
Upvotes: 1
Reputation: 50173
You can define the constant value with select
statement :
INSERT INTO table1 (field1,field2,field3,field4,field5)
SELECT "customvalue1", field2, field3, field4, field5
FROM table2
WHERE condition=true;
Upvotes: 4