1

I want to copy datas from the first table to another table. Both tables have common columns. This is the representation

table1 has already data. table 1:

id | employeeName | sectionCode | teamCode| day1 | day2 |
1     eric            400            315

and table2 has no data yet. table 2:

id | employeeName | sectionCode | teamCode | day1_a | day_b |

i want to copy the data inside table 1 to table 2 but only the common columns. So that i don't need to enter data in table 2 manually.

i tried this question but it's different.

Gelxxxx
  • 161
  • 1
  • 17
  • Possible duplicate of [How to copy data from one table to another new table in MySQL?](https://stackoverflow.com/questions/7482443/how-to-copy-data-from-one-table-to-another-new-table-in-mysql) – Shivendra Singh Jun 24 '19 at 07:17

1 Answers1

2

Explicity name the "common" columns in the target table and select only those:

insert into table2 (id, employeeName, sectionCode, teamCode)
select id, employeeName, sectionCode, teamCode
from table1

This assumes that the other columns are defined with default value or are nullable. If not, or alternatively, you can omit naming the target columns if you provide values (or expressions) for the other columns so all columns have values provided for them:

insert into table2
select id, employeeName, sectionCode, teamCode, 'foo', 'bar'
from table1
Bohemian
  • 412,405
  • 93
  • 575
  • 722