如何在 MySQL 中从值中删除部分文本?
mysqlmysqli database更新于 2024/12/25 17:48:17
为了从值中删除部分文本,您可以使用 MySQL 中的 REPLACE()。以下是语法 −
update yourTableName set yourColumnName = replace(yourColumnName ,'yourValue ', '' );
首先我们创建一个表 −
mysql> create table removePartialTextDemo -> ( -> JavaVersionDetails varchar(100) -> ); Query OK, 0 rows affected (0.51 sec)
下面是使用 insert 命令在表中插入一些记录的查询 −
mysql> insert into removePartialTextDemo values('Java Version 1.0');
Query OK, 1 row affected (0.50 sec)
mysql> insert into removePartialTextDemo values('Java Version 1.1');
Query OK, 1 row affected (0.23 sec)
mysql> insert into removePartialTextDemo values('Java Version 1.2');
Query OK, 1 row affected (0.16 sec)
mysql> insert into removePartialTextDemo values('Java Version 1.3');
Query OK, 1 row affected (0.27 sec)
mysql> insert into removePartialTextDemo values('Java Version 1.4');
Query OK, 1 row affected (0.15 sec)
mysql> insert into removePartialTextDemo values('Java Version 7');
Query OK, 1 row affected (0.11 sec)
以下是使用 select 语句显示表中的所有记录的查询 −
mysql> select * from removePartialTextDemo;
这将产生以下输出 −
+--------------------+ | JavaVersionDetails | +--------------------+ | Java Version 1.0 | | Java Version 1.1 | | Java Version 1.2 | | Java Version 1.3 | | Java Version 1.4 | | Java Version 7 | +--------------------+ 6 rows in set (0.00 sec)
以下是从值中删除部分文本的查询。我们在这里将版本号分隔开 −
mysql> update removePartialTextDemo set JavaVersionDetails = replace(JavaVersionDetails,'Java Version ',''); Query OK, 6 rows affected (0.09 sec) Rows matched: 6 Changed: 6 Warnings: 0
让我们显示表中的所有记录以检查部分文本是否已被删除 −
mysql> select * from removePartialTextDemo;
这将产生以下输出 −
+--------------------+ | JavaVersionDetails | +--------------------+ | 1.0 | | 1.1 | | 1.2 | | 1.3 | | 1.4 | | 7 | +--------------------+ 6 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

