2011-12-27 148 views
1

我想要創建單獨的行,其中包含每個單元格中的一個單詞。這些單詞當前存儲在一列中的多個單元格中。將逗號分隔的值拆分爲單獨的行

下面的格式,我有

一個
播放,遊戲,在線,自由,樂趣
樂趣,遊戲,街機,在線
賽車,搞笑,玩

待轉換成以下

B
個 玩
遊戲
在線
免費
樂趣
街機
賽車
搞笑

請注意,如果一個字不應該重複已創建一行。

+0

定義 「細胞」,因爲數據庫沒有細胞。這是在電子表格中嗎? – 2011-12-27 19:37:27

+0

哪個編。郎。你在用嗎? – 2011-12-27 19:41:32

+0

Mysql無法做到這一點(但postgres至少可以) – Bohemian 2013-06-25 14:53:04

回答

0

這是通常比SQL更好的東西,比如java。

僞代碼可能是:

List<String> names = jdbcTemplate.query("select A from your_table", new RowMapper() { 
    public Object mapRow(ResultSet resultSet, int i) throws SQLException { 
     return resultSet.getString(1); 
    } 
}); 

for (String name : names) { 
    String[] strings = name.split("[\\w,]"); 
    for (int i = 0; i < strings.length; i++) { 
     String string = strings[i]; 
     jdbcTemplate.update("insert ignore into new_table (B) values (?)", string); 
    } 

}