开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 6 天,点击查看活动详情
一、前言
大家好,今天文章的内容是几个文本函数的记录:
SubString_Index( )MID( )SubStr( )SubString( )
二、内容
SubString_Index
SubString_Index()函数用于截取字符串,具体语法如下:
SubString_Index(str, delim, count)
记录:
- 参数
str是待截取的字符串,delim是要截取字符串str所依据的字符,而count则是截取字符的位置。 - 首先,我们根据字符串
delim来将主串str分割为若干部分,接着 - 如果参数
count是正数,那么返回将第count个delim之前的所有字符 - 如果参数
count是负数,那么返回将倒数第count个delim之后的所有字符
举几个例子:
mysql> select SubString_Index('12.34.56.789', '.', 1);
+-----------------------------------------+
| SubString_Index('12.34.56.789', '.', 1) |
+-----------------------------------------+
| 12 |
+-----------------------------------------+
1 row in set (0.00 sec)
mysql> select SubString_Index('12.34.56.789', '.', 2);
+-----------------------------------------+
| SubString_Index('12.34.56.789', '.', 2) |
+-----------------------------------------+
| 12.34 |
+-----------------------------------------+
1 row in set (0.00 sec)
mysql> select SubString_Index('12.34.56.789', '.', 3);
+-----------------------------------------+
| SubString_Index('12.34.56.789', '.', 3) |
+-----------------------------------------+
| 12.34.56 |
+-----------------------------------------+
1 row in set (0.00 sec)
mysql> select SubString_Index('12.34.56.789', '.', 4);
+-----------------------------------------+
| SubString_Index('12.34.56.789', '.', 4) |
+-----------------------------------------+
| 12.34.56.789 |
+-----------------------------------------+
1 row in set (0.00 sec)
下面是count为负数的情况:
mysql> select SubString_Index('12.34.56.789', '.', -1);
+------------------------------------------+
| SubString_Index('12.34.56.789', '.', -1) |
+------------------------------------------+
| 789 |
+------------------------------------------+
1 row in set (0.00 sec)
mysql> select SubString_Index('12.34.56.789', '.', -2);
+------------------------------------------+
| SubString_Index('12.34.56.789', '.', -2) |
+------------------------------------------+
| 56.789 |
+------------------------------------------+
1 row in set (0.00 sec)
mysql> select SubString_Index('12.34.56.789', '.', -3);
+------------------------------------------+
| SubString_Index('12.34.56.789', '.', -3) |
+------------------------------------------+
| 34.56.789 |
+------------------------------------------+
1 row in set (0.00 sec)
mysql> select SubString_Index('12.34.56.789', '.', -4);
+------------------------------------------+
| SubString_Index('12.34.56.789', '.', -4) |
+------------------------------------------+
| 12.34.56.789 |
+------------------------------------------+
1 row in set (0.00 sec)
MID、SubStr 和 SubString
在 MySQL 中,SubStr()函数用于分割字符串,需要注意,函数SubString() 是它的同义词。这两个函数的用法相同,
记录如下:
SubStr(str, pos):在字符串str中返回从位置pos开始的子串- 也可以这样用:
SubStr(str FROM pos)
举几个例子:
mysql> select SubStr('sunrise',0);
+---------------------+
| SubStr('sunrise',0) |
+---------------------+
| |
+---------------------+
1 row in set (0.00 sec)
mysql> select SubStr('sunrise',1);
+---------------------+
| SubStr('sunrise',1) |
+---------------------+
| sunrise |
+---------------------+
1 row in set (0.00 sec)
mysql> select SubStr('sunrise',4);
+---------------------+
| SubStr('sunrise',4) |
+---------------------+
| rise |
+---------------------+
1 row in set (0.00 sec)
SubStr(str, pos, len):返回字符串str从位置pos开始的len个字符的子串- 也可以这样用:
SubStr(str FROM pos FOR len)
举几个例子:
mysql> select SubStr('highway', 1, 4);
+-------------------------+
| SubStr('highway', 1, 4) |
+-------------------------+
| high |
+-------------------------+
1 row in set (0.00 sec)
mysql> select SubStr('highway', 5, 3);
+-------------------------+
| SubStr('highway', 5, 3) |
+-------------------------+
| way |
+-------------------------+
1 row in set (0.00 sec)
mysql> select SubStr('highway', -7, 4);
+--------------------------+
| SubStr('highway', -7, 4) |
+--------------------------+
| high |
+--------------------------+
1 row in set (0.00 sec)
mysql> select SubStr('highway', -3, 3);
+--------------------------+
| SubStr('highway', -3, 3) |
+--------------------------+
| way |
+--------------------------+
1 row in set (0.00 sec)
MID(str, pos, len)函数用于返回字符串str从位置pos开始的len个字符的子串。效果同SubStr(str, pos, len)一样。
代码如下:
mysql> SELECT MID('highway', 1, 4);
+----------------------+
| MID('highway', 1, 4) |
+----------------------+
| high |
+----------------------+
1 row in set (0.00 sec)
mysql> SELECT MID('highway', 5, 3);
+----------------------+
| MID('highway', 5, 3) |
+----------------------+
| way |
+----------------------+
1 row in set (0.00 sec)
三、后话
好了,今天的文章内容就到这里,感谢观看。