在方括号中移除文本的 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)
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP