为此,请使用REGEXP_REPLACE()。让我们首先创建一个表-
mysql> create table DemoTable1595 -> ( -> StudentCode varchar(50) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1595 values('200 John'); mysql> insert into DemoTable1595 values('101 Carol/400 Taylor'); mysql> insert into DemoTable1595 values('101 302 405 Sam/9870');
使用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忽略数字并仅获取String和'/'的查询-
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)