MySQL 查询删除带有数字的 VARCHAR 字符串中连字符后的数字
mysqlmysqli database更新于 2025/9/29 13:32:17
为此,请使用 SUBSTRING_INDEX()。让我们首先创建一个表 −
mysql> create table DemoTable2040 -> ( -> StudentCode varchar(20) -> ); Query OK, 0 rows affected (0.85 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable2040 values('John-232');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable2040 values('Carol-901');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable2040 values('David-987');
Query OK, 1 row affected (0.21 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable2040;
这将产生以下输出 −
+-------------+ | StudentCode | +-------------+ | John-232 | | Carol-901 | | David-987 | +-------------+ 3 rows in set (0.00 sec)
这是删除连字符后的数字的查询 −
mysql> update DemoTable2040 -> set StudentCode=substring_index(StudentCode,'-',1); Query OK, 3 rows affected (0.21 sec) Rows matched: 3 Changed: 3 Warnings: 0
让我们再次检查表记录 −
mysql> select *from DemoTable2040;
这将产生以下输出 −
+-------------+ | StudentCode | +-------------+ | John | | Carol | | David | +-------------+ 3 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

