2011-12-27 224 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); 
    } 

}