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