如何从开始于MySQL中特定位置的字符串中提取子字符串?
为此,您可以使用该mid()
方法。以下是语法-
select mid(yourColumnName, yourPositionToStart, yourEndValue) as anyAliasName from yourTableName;
让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Title text -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('My best framework is Spring and Hibernate');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+-------------------------------------------+ | Title | +-------------------------------------------+ | My best framework is Spring and Hibernate | +-------------------------------------------+ 1 row in set (0.00 sec)
这是获取MySQL中字符串的一部分的查询。我们正在尝试从字符21开始获得30个字符。如果21之后的字符数不是30,则仅显示剩余字符-
mysql> select mid(Title,21,30) as partofString from DemoTable;
输出结果
+-----------------------+ | partofString | +-----------------------+ | Spring and Hibernate | +-----------------------+ 1 row in set (0.00 sec)