MySQL从csv字符串中提取数据并插入第二个表

时间:2011-10-04 11:54:14

标签: mysql

我有一个表,其中一列是一个字符串,元素用分号分隔。每行最多有K个元素。一个例子在表1中。

我必须在分号之间提取值并将它们插入到一个新表中,其中每个元素都有其列,如表2所示。

我的第一种方法是用Java创建一个例程(数据库已经被我编写的程序用来进行分析),但有没有一种方法只在MySQL中做到这一点? 我找到了这两个例子(1,2),但我的问题有点不同。我想从第一个表中提取一个值数组并将其插入第二个表中,但我不知道这是否可行。

非常感谢任何建议,


Table1
*****************************
* ID * Col1                 *
*****************************
* 1  * abc;def;ghi;lmn;opq  *
* 2  * agf;hdgk;jsud;       *
*    *                      *
* n  * jd;l;qpekd;kdh;      *
*****************************

Table2
*****************************************
* ID * Col1 * Col2 * Col3 *      * ColK *
*****************************************
* 1  * abc  * def  * ghi  *      *      *
* 2  * agf  * hdgk * jsud *      *      *
*    *      *      *      *      *      *
* n  * jd   * l    * qpekd*      *      *
*****************************************

1)Insert data from another table with a loop in mysql

2)mysql :: insert into table, data from another table?


不幸的是我不得不用Java做的工作,如果有人会发现同样的问题,我会留下解决方案。但是关于如何在MySQL中执行此操作的问题仍然存在

public void analyse_Logs(Connection as DB_Connection) {
    String MySQL_String, messageString;
    Long nInitial, nFinal, iRow;

    try{ // begin try-catch for Query_Connection
        this.checkConnection();
        Statement MySQL_Statement = DB_Connection.createStatement();

                try { // begin try-finally for Query_Statement
                    MySQL_String = "SELECT message FROM logs";
                    ResultSet MySQL_ResultSet = MySQL_Statement.executeQuery(MySQL_String);
                    try { // begin try-finally for Query_ResultSet

                        iRow = Long.Valueof(0);
                        while (MySQL_ResultSet.next()) {
                            messageString = MySQL_ResultSet.getString("message");   //for each row, extract log message
                            this.logMessageAnalysis(iRow, messageString);           //call log analysis routine
                            iRow = iRow+1;
                        }

                    } finally {
                        try { MySQL_ResultSet.close(); }
                        catch (SQLException ex) { System.out.println("Error - " + ex.toString()); }
                    } // end try-finally for Query_ResultSet

                } finally {
                    try { MySQL_Statement.close(); }
                    catch (SQLException ex) { System.out.println("Error - " + ex.toString()); }
                } // end try finally for Query_Statement

            } catch(SQLException ex) {
                    System.out.println("Error - " + ex.toString());
            } // end try-catch for Query_Connection
}

public void logMessageAnalysis(Long iRow, String logMessage, Connection DB_Connection){
    //split string in array
    String inputArray[] = logMessage.split(";");
    String[] outputArray = new String[16];

    outputArray[0] = String.valueOf(iRow);
    for(int i=1; i<inputArray.length; i=i+1){
        if (inputArray[i].length() > 45) {
            inputArray[i] = inputArray[i].substring(0, 45);
        }
        outputArray[i] = inputArray[i];
    }

    try{ // begin try-catch for Query_Connection
        this.checkConnection();
        Statement MySQL_Statement = DB_Connection.createStatement();

        try { // begin try-finally for Query_Statement
            String Query_String = "INSERT INTO logs_2 VALUES ('";

            for (int i=0; i<15; i=i+1) {
                Query_String = Query_String + outputArray[i] + "','";
            }

            Query_String = Query_String + outputArray[15] + "')";

            MySQL_Statement.executeUpdate(Query_String);
        } finally {
            try { MySQL_Statement.close(); }
            catch (SQLException ex) { System.out.println("Error - " + ex.toString()); }
        } // end try finally for Query_Statement

    } catch(SQLException ex) {
            System.out.println("Error - " + ex.toString());
    } // end try-catch for Query_Connection

}

1 个答案:

答案 0 :(得分:0)

INSERT INTO table2 (ID, col1, col2, col3, col4)
  SELECT 
    t1.id
    , substring_index(t1.col1,';',1)
    , substring_index(t1.col1,';',2)
    , substring_index(t1.col1,';',3)
    , substring_index(t1.col1,';',4)
   FROM table1 t1 

请参阅:http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring-index