从 MySQL 列中删除括号内的尾随数字
mysqlmysqli database更新于 2025/12/20 18:52:17
为此,请使用 trim() 和 substring()。让我们首先创建一个表 −
mysql> create table DemoTable ( Name varchar(100) ); Query OK, 0 rows affected (0.80 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable values('1stJohn');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values('1stJohn (7)');
Query OK, 1 row affected (0.65 sec)
mysql> insert into DemoTable values('2ndSam');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('2ndSam (4)');
Query OK, 1 row affected (0.15 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+-------------+ | Name | +-------------+ | 1stJohn | | 1stJohn (7) | | 2ndSam | | 2ndSam (4) | +-------------+ 4 rows in set (0.00 sec)
以下是从 MySQL 列中删除括号内的尾随数字的查询 −
mysql> select trim(substring(Name, 1, (CHAR_LENGTH(Name) - LOCATE('(', REVERSE(Name))))) AS RemovingTrailingNumbers from DemoTable;
这将产生以下输出 −
+-------------------------+ | RemovingTrailingNumbers | +-------------------------+ | 1stJohn | | 1stJohn | | 2ndSam | | 2ndSam | +-------------------------+ 4 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

