MySQL字符串截取

MySQL字符串截取函数

left(), right(), substring(), substring_index(), mid(), substr()。其中,mid(), substr() 等价于 substring() 函数,substring() 的功能非常强大和灵活。

1. 字符串截取:left(str, length)

mysql> select left('helloguy.com', 3);  
+-------------------------+  
| left('helloguy.com', 3) |  
+-------------------------+  
| sql                     |  
+-------------------------+  

2. 字符串截取:right(str, length)

mysql> select right('helloguy.com', 3);  
+--------------------------+  
| right('helloguy.com', 3) |  
+--------------------------+  
| com                      |  
+--------------------------+  

3. 字符串截取:substring(str, pos); substring(str, pos, len)

3.1 从字符串的第 4 个字符位置开始取,直到结束。

mysql> select substring('helloguy.com', 4);  
+------------------------------+  
| substring('helloguy.com', 4) |  
+------------------------------+  
| study.com                    |  
+------------------------------+  

3.2 从字符串的第 4 个字符位置开始取,只取 2 个字符。

mysql> select substring('helloguy.com', 4, 2);  
+---------------------------------+  
| substring('helloguy.com', 4, 2) |  
+---------------------------------+  
| st                              |  
+---------------------------------+  

3.3 从字符串的第 4 个字符位置(倒数)开始取,直到结束。

mysql> select substring('helloguy.com', -4);  
+-------------------------------+  
| substring('helloguy.com', -4) |  
+-------------------------------+  
| .com                          |  
+-------------------------------+  

3.4 从字符串的第 4 个字符位置(倒数)开始取,只取 2 个字符。

mysql> select substring('helloguy.com', -4, 2);  
+----------------------------------+  
| substring('helloguy.com', -4, 2) |  
+----------------------------------+  
| .c                               |  
+----------------------------------+  

我们注意到在函数 substring(str,pos, len)中, pos 可以是负值,但 len 不能取负值。

4. 字符串截取:substring_index(str,delim,count)

4.1 截取第二个 '.' 之前的所有字符。

mysql> select substring_index('www.helloguy.com.cn', '.', 2);  
+------------------------------------------------+  
| substring_index('www.helloguy.com.cn', '.', 2) |  
+------------------------------------------------+  
| www.helloguy                                   |  
+------------------------------------------------+  

4.2 截取第二个 '.' (倒数)之后的所有字符。

mysql> select substring_index('www.helloguy.com.cn', '.', -2);  
+-------------------------------------------------+  
| substring_index('www.helloguy.com.cn', '.', -2) |  
+-------------------------------------------------+  
| com.cn                                          |  
+-------------------------------------------------+ 

4.3 如果在字符串中找不到 delim 参数指定的值,就返回整个字符串

mysql> select substring_index('www.helloguy.com.cn', '.coc', 1);  
+---------------------------------------------------+  
| substring_index('www.helloguy.com.cn', '.coc', 1) |  
+---------------------------------------------------+  
| www.helloguy.com.cn                               |  
+---------------------------------------------------+  

4.4 截取一个表某个字段数据的中间值 如该字段数据为 1,2,3

mysql> select substring_index(substring_index(该字段, ',', 2) , ',', -1) from 表名;    
+--------------------------------------------------------------+    
| substring_index(substring_index(该字段, ',', 2);  , ',', -1)|    
+--------------------------------------------------------------+    
| 2                                        |    
+--------------------------------------------------------------+