获取基于MySQL中字符的字符串的一部分?

让我们首先创建一个表-

mysql> create table DemoTable
   -> (
   -> Code varchar(100)
   -> );

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

mysql> insert into DemoTable values('/101/102/106');

mysql> insert into DemoTable values('/110/111/101');

mysql> insert into DemoTable values('/111/114/201');

mysql> insert into DemoTable values('/111/118');

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

mysql> select *from DemoTable;

输出结果

这将产生以下输出-

+--------------+
| Code         |
+--------------+
| /101/102/106 |
| /110/111/101 |
| /111/114/201 |
| /111/118     |
+--------------+
4 rows in set (0.00 sec)

这是根据字符获取字符串一部分的查询-

mysql> select SUBSTRING_INDEX(SUBSTRING_INDEX(Code,'/',3),'/',-1) from DemoTable;

输出结果

这将产生以下输出-

+-----------------------------------------------------+
| SUBSTRING_INDEX(SUBSTRING_INDEX(Code,'/',3),'/',-1) |
+-----------------------------------------------------+
| 102                                                 |
| 111                                                 |
| 114                                                 |
| 118                                                 |
+-----------------------------------------------------+
4 rows in set (0.00 sec)