Home  >  Article  >  Database  >  mysql implements string interception

mysql implements string interception

王林
王林forward
2020-01-26 21:44:045168browse

mysql implements string interception

First we need to understand the string interception functions:

left(), right(), substring(), substring_index(). There are also mid(), substr(). Among them, mid() and substr() are equivalent to the substring() function, and the function of substring() is very powerful and flexible.

(Free learning video tutorial recommendation: mysql video tutorial)

Specific examples are as follows:

1. 字符串截取:left(str, length)  
mysql> select left('sqlstudy.com', 3);  
+-------------------------+  
| left('sqlstudy.com', 3) |  
+-------------------------+  
| sql                     |  
+-------------------------+  
2. 字符串截取:right(str, length)  
mysql> select right('sqlstudy.com', 3);  
+--------------------------+  
| right('sqlstudy.com', 3) |  
+--------------------------+  
| com                      |  
+--------------------------+  
3. 字符串截取:substring(str, pos); substring(str, pos, len)  
3.1 从字符串的第 4 个字符位置开始取,直到结束。  
mysql> select substring('sqlstudy.com', 4);  
+------------------------------+  
| substring('sqlstudy.com', 4) |  
+------------------------------+  
| study.com                    |  
+------------------------------+  
3.2 从字符串的第 4 个字符位置开始取,只取 2 个字符。  
mysql> select substring('sqlstudy.com', 4, 2);  
+---------------------------------+  
| substring('sqlstudy.com', 4, 2) |  
+---------------------------------+  
| st                              |  
+---------------------------------+  
3.3 从字符串的第 4 个字符位置(倒数)开始取,直到结束。  
mysql> select substring('sqlstudy.com', -4);  
+-------------------------------+  
| substring('sqlstudy.com', -4) |  
+-------------------------------+  
| .com                          |  
+-------------------------------+  
3.4 从字符串的第 4 个字符位置(倒数)开始取,只取 2 个字符。  
mysql> select substring('sqlstudy.com', -4, 2);  
+----------------------------------+  
| substring('sqlstudy.com', -4, 2) |  
+----------------------------------+  
| .c                               |  
+----------------------------------+  
我们注意到在函数 substring(str,pos, len)中, pos 可以是负值,但 len 不能取负值。  
4. 字符串截取:substring_index(str,delim,count)  
4.1 截取第二个 '.' 之前的所有字符。  
mysql> select substring_index('www.sqlstudy.com.cn', '.', 2);  
+------------------------------------------------+  
| substring_index('www.sqlstudy.com.cn', '.', 2) |  
+------------------------------------------------+  
| www.sqlstudy                                   |  
+------------------------------------------------+  
4.2 截取第二个 '.' (倒数)之后的所有字符。  
mysql> select substring_index('www.sqlstudy.com.cn', '.', -2);  
+-------------------------------------------------+  
| substring_index('www.sqlstudy.com.cn', '.', -2) |  
+-------------------------------------------------+  
| com.cn                                          |  
+-------------------------------------------------+  
4.3 如果在字符串中找不到 delim 参数指定的值,就返回整个字符串  
mysql> select substring_index('www.sqlstudy.com.cn', '.coc', 1);  
+---------------------------------------------------+  
| substring_index('www.sqlstudy.com.cn', '.coc', 1) |  
+---------------------------------------------------+  
| www.sqlstudy.com.cn                               |  
+---------------------------------------------------+  
 4.4 截取一个表某个字段数据的中间值 如该字段数据为  1,2,3  
mysql> select substring_index(substring_index(该字段, ',', 2) , ',', -1) from 表名;    
+--------------------------------------------------------------+    
| substring_index(substring_index(该字段, ',', 2);  , ',', -1)|    
+--------------------------------------------------------------+    
| 2                                        |    
+--------------------------------------------------------------+

Recommended related article tutorials: mysql tutorial

The above is the detailed content of mysql implements string interception. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete