mysql - 使用MYSQL删除字符串中不需要的字符

标签 mysql sql

我在 MYSQL 表中有一个这样的字符串。

"Collectibles
 >
 Decorative Collectibles
 >
 Decorative Collectible Brands
 >
 Department 56
 >
 Ornaments
 [ Changecategory ]"

我需要将其转换为(用逗号分隔)

Collectibles,Decorative Collectibles,Decorative Collectible Brands
,Department 56,Ornaments

我尝试用逗号替换回车符,但它不起作用。

最佳答案

试试这个。

SELECT 
REPLACE(
SUBSTRING_INDEX(
'Collectibles
 >
 Decorative Collectibles
 >
 Decorative Collectible Brands
 >
 Department 56
 >
 Ornaments
 [ Changecategory ]', '[', 1),
'\n >\n',',') output;

输出:

mysql> SELECT
    -> REPLACE(
    -> SUBSTRING_INDEX(
    -> 'Collectibles
    '>  >
    '>  Decorative Collectibles
    '>  >
    '>  Decorative Collectible Brands
    '>  >
    '>  Department 56
    '>  >
    '>  Ornaments
    '>  [ Changecategory ]', '[', 1),
    -> '\n >\n',',') output;
+--------------------------------------------------------------------------------------------------+
| output                                                                                           |
+--------------------------------------------------------------------------------------------------+
| Collectibles, Decorative Collectibles, Decorative Collectible Brands, Department 56, Ornaments
  |
+--------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

关于mysql - 使用MYSQL删除字符串中不需要的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35882562/

相关文章:

mysql - 如何进行低 RAM 完全交叉连接?

mysql - 如何用MySQL中以前的非零值替换零值?

mysql - 使用 "group by ... with rollup"时区分NULL

sql - 在 sql 中使用 ALL 命令而不是 MAX 命令

mysql - 在数据库中存储多条数据的两种不同方法。我应该选择哪个?

c# - 查询以 int 数据类型对 Access 数据库中的字段求和

MySQL - 选择检索最后的日期时间

php - 获取所有超过 15 分钟的 WP 帖子

python - 无法回滚带有 MySQL 非事务性更改表的 Django

mysql - 使用Mysql数据库从两个SQL表中具体选择数据