0
votes

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?

3

3 Answers

4
votes

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;
1
votes

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

1
votes

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;