Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy Data From One Table To Another AND Adding Additional Data

Tags:

mysql

I have a temp table and a regular table in my database. The column name and types are identical, except the regular table has an extra field. I am trying to write a query that copies the information from the temp table into the regular table and adds data into the addition field all in one query.

I understand how to copy columns from one table to another (e.g. INSERT INTO TABLE 1 (col 1, etc..) SELECT TABLE2), but how do i do this AND then add in a the value for the new field?

Thanks for your help.

like image 887
Javit Avatar asked Feb 03 '23 21:02

Javit


2 Answers

 INSERT INTO TABLE1 (col 1, col2,..., the_extra_col) SELECT *, NULL from TABLE2

or

 INSERT INTO TABLE1 (col 1, col2,..., the_extra_col) SELECT *, the_default_date_here from TABLE2
like image 87
Nishant Avatar answered Feb 05 '23 11:02

Nishant


If you want your values

INSERT INTO TABLE1 (col 1, col2,..., the_extra_col) SELECT *,concat('".$value."') as value1 from TABLE2

Date means just put now() INSERT INTO TABLE1 (col 1, col2,..., the_extra_col) SELECT *,now() from TABLE2

like image 33
Tamilarasan Avatar answered Feb 05 '23 11:02

Tamilarasan