在方括号中移除文本的 MySQL 查询?


我们首先创建一个表 -

mysql> create table DemoTable
   -> (
   -> Name text
   -> );
Query OK, 0 rows affected (0.47 sec)

使用 insert 命令在表中插入一些记录 -

mysql> insert into DemoTable values('John [John] Smith');
Query OK, 1 row affected (0.30 sec)
mysql> insert into DemoTable values('[Carol] Carol Taylor');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('David [Miller] Miller');
Query OK, 1 row affected (0.14 sec)

使用 select 语句从表中显示所有记录 -

mysql> select *from DemoTable;

这将产生以下输出 -

+-----------------------+
| Name                  |
+-----------------------+
| John [John] Smith     |
| [Carol] Carol Taylor  |
| David [Miller] Miller |
+-----------------------+
3 rows in set (0.00 sec)

以下是用于移除方括号中文本的查询 -

mysql> select replace(Name,substring(Name,locate('[', Name), LENGTH(Name)
   -> - locate(']', reverse(Name)) - locate('[', Name) + 2), '') as Name
   -> from DemoTable;

这将产生以下输出 -

+---------------+
| Name          |
+---------------+
| John Smith    |
| Carol Taylor  |
| David Miller  |
+---------------+
3 rows in set (0.03 sec)

更新于: 2019 年 12 月 12 日

810 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.