如何在 MySQL 中获取字符最后一次出现之前的所有内容?

mysqlmysqli database更新于 2025/5/23 8:12:17

您可以使用以下语法。以下是语法 −

update yourTableName
set yourColumnName=REVERSE(SUBSTRING(REVERSE(yourColumnName),
INSTR(REVERSE(yourColumnName), '.')));

首先我们创建一个表 −

mysql> create table DemoTable
-> (
-> Words text
-> );
Query OK, 0 rows affected (0.51 sec)

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

mysql> insert into DemoTable values('Value1. Value2 .Value3.Value4.Value5');
Query OK, 1 row affected (0.22 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable;

输出

这将产生以下输出 −

+--------------------------------------+
| Words                                |
+--------------------------------------+
| Value1. Value2 .Value3.Value4.Value5 |
+--------------------------------------+
1 row in set (0.00 sec)

以下查询用于获取 MySQL 中某个字符最后一次出现之前的所有内容−

mysql> update DemoTable
   -> set Words=REVERSE(SUBSTRING(REVERSE(Words), INSTR(REVERSE(Words),'.')));
Query OK, 1 row affected (0.13 sec)
Rows matched: 1 Changed: 1 Warnings: 0

让我们再次检查表记录 −

mysql> select *from DemoTable;

输出

这将产生以下输出 −

+--------------------------------+
| Words                          |
+--------------------------------+
| Value1. Value2 .Value3.Value4. |
+--------------------------------+
1 row in set (0.00 sec)

相关文章


有用资源