使用 MySQL REGEXP 忽略数字并仅获取字符串和"/"

mysqlmysqli database更新于 2024/8/11 6:04:17

为此,请使用 REGEXP_REPLACE()。让我们首先创建一个表 −

mysql> create table DemoTable1595
   -> (
   -> StudentCode varchar(50)
   -> );
Query OK, 0 rows affected (0.44 sec)

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

mysql> insert into DemoTable1595 values('200 John');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable1595 values('101 Carol/400 Taylor');
Query OK, 1 row affected (0.72 sec)
mysql> insert into DemoTable1595 values('101 302 405 Sam/9870');
Query OK, 1 row affected (0.28 sec)

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

mysql> select * from DemoTable1595;

这将产生以下输出 −

+----------------------+
| StudentCode          |
+----------------------+
| 200 John             |
| 101 Carol/400 Taylor |
| 101 302 405 Sam/9870 |
+----------------------+
3 rows in set (0.00 sec)

这是使用 MySQL REGEXP 忽略数字并仅获取字符串和 '/' 的查询 −

mysql> select regexp_replace(StudentCode,' *[0-9]+([.,][0-9]+)?(/[0-9]+([.,][0-9]+)?)? *', '') as `GetOnlyStringand/` from DemoTable1595;

这将产生以下输出 −

+-------------------+
| GetOnlyStringand/ |
+-------------------+
| John              |
| Carol/Taylor      |
| Sam/              |
+-------------------+
3 rows in set (0.00 sec)

相关文章


有用资源