在方括號中移除文字的 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.