将逗号分隔的值拆分为单独的行 [英] Split comma separated values into individual rows

查看:38
本文介绍了将逗号分隔的值拆分为单独的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建单独的行,在每个单元格中包含一个单词.这些词目前存储在一列的多个单元格中.

I want to create separate rows which will contain one word in each cell. These words are currently stored in several cells in one column.

下面是我的格式

A
玩,游戏,在线,免费,有趣
好玩,玩,街机,在线
赛车,搞笑,玩耍

A
play, games, online, free, fun
fun, play, arcade, online
racing, funny, play

要转换成下面的



游戏
在线
免费
好玩
街机
赛车
搞笑

B
play
games
online
free
fun
arcade
racing
funny

请注意,如果已经为一个单词创建了一行,则不应重复.

Please note, if one row is already created for one word it should not be repeated.

推荐答案

这通常在 SQL 以外的其他方面做得更好,比如 Java.

This is something that is typically better done in something other than SQL, like 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);
    }

}

这篇关于将逗号分隔的值拆分为单独的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆