【问题标题】:copy data from one tables to another one将数据从一个表复制到另一个表
【发布时间】:2014-12-10 09:33:14
【问题描述】:

我有两个表 OldDocumentsDocuments,我想从一个表中导入数据,OldDocument 并将其复制到新表中,因为这两个表的列名编号和名称都不相同。

这是我要导入新表的列

旧文档

Id(PK)
文档(BLOB)
文件名(VARCHAR)
文档类型(VARCHAR)
user_Id(FK)

文档

ID (PK)
文档内容 (BLOB)
文件名 (VARCHAR)
DocType(VARCHAR)
user_Id
(FK)

我需要一个查询,该查询将从一个表中进行选择并将这些列复制到新表中。类似的东西

    INSERT INTO DOCUMENT(ID,document_content, fileName , DocType, user_Id) 
    VALUES (get data from the old table)

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    您可以插入另一个选择的结果。这是文档:http://www.w3schools.com/sql/sql_insert_into_select.asp

    例如:INSERT INTO table2 (column_name(s)) SELECT column_name(s) FROM table1;

    【讨论】:

      【解决方案2】:
      INSERT INTO Document (Id,Document_content,fileName,DocType,user_Id)
      SELECT Id,Document,fileName,DocumentType,user_Id
      FROM OldDocument
      ;
      

      【讨论】:

        【解决方案3】:

        使用INSERT INTO ... SELECT:

        INSERT INTO DOCUMENT(ID,document_content, fileName , DocType, user_Id) 
        SELECT ID, Document, fileName, DocumentType, user_id FROM OldDocument;
        

        【讨论】:

          【解决方案4】:
          INSERT INTO Document
          SELECT Id,Document,fileName,DocumentType,user_Id
          FROM OldDocument
          

          【讨论】:

            【解决方案5】:

            尝试使用INSERT INTO ... SELECT

            http://dev.mysql.com/doc/refman/5.0/en/insert-select.html

            INSERT INTO Document (ID, document_content, fileName , DocType, user_Id) 
            SELECT Id, Document, filename, DocumentType, user_Id FROM OldDocument
            

            【讨论】:

              【解决方案6】:

              试试这个:

              INSERT INTO DOCUMENT(ID,document_content, fileName , DocType, user_Id) 
                  select Id, Document, filename, DocumentType, user_Id from OldDocument
              

              【讨论】:

                【解决方案7】:

                这是此类问题正确答案的一般格式

                INSERT INTO table2
                (column_name(s))
                SELECT column_name(s)
                FROM table1;
                

                来源:http://www.w3schools.com/sql/sql_insert_into_select.asp

                【讨论】:

                  猜你喜欢
                  • 2012-11-24
                  • 2011-05-16
                  • 2023-03-16
                  • 1970-01-01
                  • 2012-01-07
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多