Copy tables with different column name MySQL

本秂侑毒 提交于 2020-01-02 05:46:08

问题


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.


回答1:


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



回答2:


You can use an insert-select statement:

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


来源:https://stackoverflow.com/questions/5753159/copy-tables-with-different-column-name-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!