让我们首先创建一个表-
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');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+--------------+ | Code | +--------------+ | /101/102/106 | | /110/111/101 | | /111/114/201 | +--------------+ 3 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 | +-----------------------------------------------------+ 3 rows in set (0.00 sec)