(编辑:jimmy 日期: 2025/11/4 浏览:2)
"htmlcode">
mysql> SELECT LEFT('www.csdn.net',5) from web_info w;
+------------------+
| LEFT('www.csdn.net',5) |
+------------------+
| www.c   |
+------------------+
1 row in set (0.00 sec)
"htmlcode">
mysql> SELECT RIGHT('www.csdn.net',5) from web_info w;
+-------------------------+
| RIGHT('www.csdn.net',5) |
+-------------------------+
| n.net     |
+-------------------------+
1 row in set (0.00 sec)
substring()是专门用来对字符串进行切分的函数,主要有两种形式:
以下是上述两类语句的SQL标准版本写法,意义相同,它更长,但更具表现力。
SUBSTRING(string FROM position); SUBSTRING(string FROM position FOR length);
下面让我们来一起看一下SUBSTRING()的两种用法示例;
1.SUBSTRING(string,position)
"整数",用于指定子串的起始字符,position可以是正整数或负整数。若position大于操作字符串的长度,则返回空字符串。例如,从“www.csdn.net”字符串中获取子字符串:“csdn.net”,子串的位置必须从5开始,如以下SELECT语句:
mysql> SELECT substring('www.csdn.net',5) from web_info w;
+-----------------------------+
| substring('www.csdn.net',5) |
+-----------------------------+
| csdn.net     |
+-----------------------------+
1 row in set (0.00 sec)
2.SUBSTRING(string,position,length)
"htmlcode">
mysql> SELECT substring('www.csdn.net',5,4) from web_info w;
+-------------------------------+
| substring('www.csdn.net',5,4) |
+-------------------------------+
| csdn       |
+-------------------------------+
1 row in set (0.00 sec)
或者通过配置position,从后往前数;SQL如下:
mysql> SELECT substring('www.csdn.net',-8,4) from web_info w;
+--------------------------------+
| substring('www.csdn.net',-8,4) |
+--------------------------------+
| csdn       |
+--------------------------------+
1 row in set (0.00 sec)
又或者通过SQL标准方式来写,SQL如下:
mysql> SELECT substring('www.csdn.net' FROM 5 FOR 4) from web_info w;
+----------------------------------------+
| substring('www.csdn.net' FROM 5 FOR 4) |
+----------------------------------------+
| csdn         |
+----------------------------------------+
1 row in set (0.00 sec)
另外,MySQL中的 mid(), substr() 等价于 substring() 函数哦!
"delim"来截取子串的函数,我们日常使用频率是很高的;
例如:‘www.csdn.net',获取第一次出现标识符'.'前面的子串,SQL如下;
mysql> SELECT SUBSTRING_INDEX('www.csdn.net','.',1) from web_info w;
+---------------------------------------+
| SUBSTRING_INDEX('www.csdn.net','.',1) |
+---------------------------------------+
| www         |
+---------------------------------------+
1 row in set (0.00 sec)
获取第一次出现标识符'.'后面的子串,SQL如下;
mysql> SELECT SUBSTRING_INDEX('www.csdn.net','.',-2) from web_info w;
+----------------------------------------+
| SUBSTRING_INDEX('www.csdn.net','.',-2) |
+----------------------------------------+
| csdn.net        |
+----------------------------------------+
1 row in set (0.00 sec)
想获取两个'.'中间的呢?可以嵌套一下,SQL如下;
mysql> SELECT SUBSTRING_INDEX(substring_index('www.csdn.net','.',2),'.',-1) from web_info w;
+---------------------------------------------------------------+
| SUBSTRING_INDEX(substring_index('www.csdn.net','.',2),'.',-1) |
+---------------------------------------------------------------+
| csdn               |
+---------------------------------------------------------------+
1 row in set (0.00 sec)
我们以表web_info为测试表,数据如下;
mysql> select * from web_info; +------+--------+----------------+---------------------+ | w_id | w_name | w_domain | pub_time | +------+--------+----------------+---------------------+ | 1 | CSDN | www.csdn.net | 2020-09-03 11:29:29 | | 5 | 百度 | www.baidu.com | 2020-09-18 14:37:38 | | 6 | 淘宝 | www.taobao.com | 2020-09-03 14:37:57 | +------+--------+----------------+---------------------+ 3 rows in set (0.00 sec)
需求1:获取web_info数据的一级域名;
注:这里仅用于举例说明,与实际会有些出入,如.com.cn那种多级后缀就需另外处理。
mysql> SELECT SUBSTRING_INDEX(w_domain,'.',-2),w.* from web_info w; +----------------------------------+------+--------+----------------+---------------------+ | SUBSTRING_INDEX(w_domain,'.',-2) | w_id | w_name | w_domain | pub_time | +----------------------------------+------+--------+----------------+---------------------+ | csdn.net | 1 | CSDN | www.csdn.net | 2020-09-03 11:29:29 | | baidu.com | 5 | 百度 | www.baidu.com | 2020-09-18 14:37:38 | | taobao.com | 6 | 淘宝 | www.taobao.com | 2020-09-03 14:37:57 | +----------------------------------+------+--------+----------------+---------------------+ 3 rows in set (0.00 sec)
需求2:把w_domain字段数据都改成一级域名;
update web_info set w_domain = SUBSTRING_INDEX(w_domain,'.',-2) ;
修改后查询:
mysql> SELECT * from web_info; +------+--------+------------+---------------------+ | w_id | w_name | w_domain | pub_time | +------+--------+------------+---------------------+ | 1 | CSDN | csdn.net | 2020-09-03 14:54:59 | | 5 | 百度 | baidu.com | 2020-09-03 14:54:59 | | 6 | 淘宝 | taobao.com | 2020-09-03 14:54:59 | +------+--------+------------+---------------------+ 3 rows in set (0.00 sec)
好了,MySQL的字符串切分函数今天就说到这儿,如果对小伙伴儿有用,请不要白嫖哦~~