简体   繁体   中英

Copy tables with different column name MySQL

I need to copy all rows from table1 matching specific columns into table2 with different columns name. For example:

  • table1 name = oldAddressBook , table1's columns name = Name,Surname,Number
  • table2 name = newAddressBook , table2's columns name = newName,newSurname,Phone

Data in columns "Name,Surname,Number" in "oldAddressBook" must fill respectively "newName,newSurname,Phone" in "newAddressBook". "oldAddressBook" and "newAddressBook" contain also other columns.

INSERT INTO newAddressBook (newName, newSurname, Phone)
SELECT name, surname, number
FROM oldAddressBook

You can use an insert-select statement:

INSERT INTO newAddressBook (`newName`, `newSurname`, `Phone`)
SELECT `Name`, `Surname`, `Number` FROM oldAddressBook;

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