【问题标题】:Export data from one database to another WITHOUT x fields?将数据从一个数据库导出到另一个没有 x 字段?
【发布时间】:2012-07-11 09:34:46
【问题描述】:

我想将数据从一个表 (table1) 移动到另一个 (table2)。它们具有几乎相同的字段,但 table1 有一些我在 table2 中不需要的额外字段(带有数据)。

所以我不能只进行 CSV 导出和导入,因为我最终会出错。所以我的问题;如何创建没有字段 X、Y 和 Z 的数据库字段的导出?

编辑

假设我有一个包含以下字段的 table1;

id, name, comment, email, date

我有table2;

id, name, comment, date

所以没有email 字段。如果我对 table1 进行 CSV 导出,我无法直接将其导入 table2,因为有一个额外的字段(电子邮件)。

【问题讨论】:

    标签: sql database phpmyadmin


    【解决方案1】:
    CREATE TABLE table2 SELECT field1, field2, etc FROM table1
    

    如果table2 已经存在,则:

    INSERT INTO table2 SELECT field1, field2, etc FROM table1
    

    【讨论】:

      【解决方案2】:

      如果您使用 sql server management studio 导出数据,则可以选择 给出查询而不是选择要导出的表。

      所以在查询中给出你需要的字段

      SSMS ->
      right click on database->Tasks->export data -> 
      
      after selecting the source and destination, you get a window called
      "Specify table copy or Query"
      

      您可以在此处选择第二个选项并提供查询

      【讨论】:

        【解决方案3】:

        插入时只需选择要插入的字段

        insert into table2 (id, name, comment, date)
            select id, name, comment, date
            from table1
        

        【讨论】:

        • 对不起。你是什​​么意思?哪些领域?之前要删除table2中的所有数据吗?
        • 我更新了我的答案。仅从 table1 中选择要插入到 table2 中的字段
        猜你喜欢
        • 2017-12-01
        • 2011-04-18
        • 1970-01-01
        • 1970-01-01
        • 2012-12-02
        • 1970-01-01
        • 1970-01-01
        • 2016-10-08
        • 1970-01-01
        相关资源
        最近更新 更多