如何将一列复制到另一个新表

时间:2011-09-25 11:35:56

标签: mysql

我的表格结构示例

current_db

ID email           name  address    phone
---------------------------------------------
1  email@email.com John  My Address  123456789
2  email@email.net Peter My Address  123456721

new_db

ID email column1  column2 column3  column4
------------------------------------------

如何仅将current_db的电子邮件地址复制到new_db

2 个答案:

答案 0 :(得分:1)

使用INSERT … SELECT语法:

INSERT INTO `new_db` ( `email` )
SELECT `email` FROM `current_db`

答案 1 :(得分:1)

认为我误解了......对我来说很早。您提到从一个数据库到另一个数据库,而不是从表到表。

如果这些表实际上是保存在单独的“数据库”中,例如重建,或者从旧的数据库移植到新数据库而你正在重组表....你必须预先聚焦到新数据库并创建表从data.table中所需列的插入到另一个。

use New_db
create table x select email from Other_Db.YourTable

然而,从重读和看到其他答案,这可能更接近你想要的

insert into OneTable ( columnX, columnY, columnZ ) values select x, y, z from OtherTable where...