Hench
Hench

Reputation: 59

How to insert rows from one table into another table in MYSQL

I have two following MySQL tables

Table A

id Name Age
1  John 25
2  Tony 30
3  Tom  35

Table B

id Name Age
1  Sue 25
2  Jane 30
3  Jessica 35

If I want to insert all table B rows into table A, how can I do it? I tried use following query

insert into table A select Name, Age from Table B

the result is error. This is because the columns do not match. But I cannot include the id column, since it will conflict the id in Table A.

Upvotes: 1

Views: 85

Answers (1)

Nick
Nick

Reputation: 147196

You just need to specify the columns you are inserting:

insert into table A (Name, Age) 
select Name, Age 
from Table B

Upvotes: 1

Related Questions