简体   繁体   中英

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
  • Primary Key: id, id is auto increment

Table B

id Name Age
1  Sue 25
2  Jane 30
3  Jessica 35
  • Primary Key: id, id is auto increment

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.

You just need to specify the columns you are inserting:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM