Stefan
Stefan

Reputation: 259

Insert into select with new value

Is it possible to copy the data of a table to an new table and add an extra value to it? So, I would like to use this query:

INSERT INTO database2.table1 (field2,field3) SELECT table2.field2,table2.field3 FROM table2;

but would also add a new variable, for example a date field: date=now()

Upvotes: 0

Views: 60

Answers (2)

fpavone
fpavone

Reputation: 269

Assuming table1 has the same field as table2 plus a field date:

INSERT INTO db.table1 (`field1`, `field2`, `date`)
SELECT 
  `field1`,
  `field2`,
  NOW() as `date`
FROM db.table2;

Upvotes: 1

Suchit kumar
Suchit kumar

Reputation: 11869

yes it can be done.try like this :

INSERT INTO database2.table1(field2,field3, date) 
SELECT table2.field2,table2.field3,now() AS date
FROM table2

Upvotes: 0

Related Questions