如何从 MySQL 中的某一列中删除所有特定字符的实例?


我们先创建一个表 -

mysql> create table DemoTable
   -> (
   -> FirstName varchar(100)
   -> );
Query OK, 0 rows affected (0.41 sec)

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

mysql> insert into DemoTable values('Adam^^^');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable values('^^^^^^^^Carol');
Query OK, 1 row affected (0.18 sec)

mysql> insert into DemoTable values('Robert^^^^^^');
Query OK, 1 row affected (0.14 sec)

使用选择语句显示表中的所有记录 -

mysql> select *from DemoTable;

输出

这将产生以下输出 -

+---------------+
| FirstName     |
+---------------+
| Adam^^^       |
| ^^^^^^^^Carol |
| Robert^^^^^^  |
+---------------+
3 rows in set (0.00 sec)

以下是从 MySQL 中的某一列中删除所有特定字符实例的查询。在此,我们要删除特殊字符 ^ 的所有实例 -

mysql> update DemoTable set FirstName=replace(FirstName,'^','');
Query OK, 3 rows affected (0.20 sec)
Rows matched: 3  Changed: 3 Warnings: 0

让我们再次检查表记录 -

mysql> select *from DemoTable;

输出

这将产生以下输出 -

+-----------+
| FirstName |
+-----------+
| Adam      |
| Carol     |
| Robert    |
+-----------+
3 rows in set (0.00 sec)

更新于: 30-6 月 2020 年

897 查看

开启您的职业

通过完成课程获得认证

开始
广告
© . All rights reserved.