一:MySQL 字符串截取相关函数
首先来看一下 MySQL 中跟字符串截取相关的函数有:
left(), right(), substring(), substring_index()、 mid(), substr()。其中 mid() 和 substr() 都是 substring() 的同义词

二:MySQL 字符串截取函数使用说明

1. left()

Name: 'LEFT'
Description:
Syntax:
LEFT(str,len)Returns the leftmost len characters from the string str, or NULL if any
argument is NULL.URL: https://dev.mysql.com/doc/refman/5.7/en/string-functions.htmlExamples:
mysql> SELECT LEFT('foobarbar', 5);-> 'fooba'##该函数从最左侧开始向右截取字符串,截取 len 指定的字符后停止,例如上面的例子中 len 为5,所以从最左侧开始向右截取5个字符

2.right()

Name: 'RIGHT'
Description:
Syntax:
RIGHT(str,len)Returns the rightmost len characters from the string str, or NULL if
any argument is NULL.URL: https://dev.mysql.com/doc/refman/5.7/en/string-functions.htmlExamples:
mysql> SELECT RIGHT('foobarbar', 4);-> 'rbar'##该函数从最右侧开始往左截取字符串,截取 len 指定的字符后停止,例如上面的例子中 len 为4,所以从最右侧开始向左截取4个字符

3.substring()

Name: 'SUBSTRING'
Description:
Syntax:
SUBSTRING(str,pos), SUBSTRING(str FROM pos), SUBSTRING(str,pos,len),
SUBSTRING(str FROM pos FOR len)The forms without a len argument return a substring from string str
starting at position pos. The forms with a len argument return a
substring len characters long from string str, starting at position
pos. The forms that use FROM are standard SQL syntax. It is also
possible to use a negative value for pos. In this case, the beginning
of the substring is pos characters from the end of the string, rather
than the beginning. A negative value may be used for pos in any of the
forms of this function. A value of 0 for pos returns an empty string.For all forms of SUBSTRING(), the position of the first character in
the string from which the substring is to be extracted is reckoned as
1.URL: https://dev.mysql.com/doc/refman/5.7/en/string-functions.htmlExamples:
mysql> SELECT SUBSTRING('Quadratically',5);-> 'ratically'
mysql> SELECT SUBSTRING('foobarbar' FROM 4);-> 'barbar'
mysql> SELECT SUBSTRING('Quadratically',5,6);-> 'ratica'
mysql> SELECT SUBSTRING('Sakila', -3);-> 'ila'
mysql> SELECT SUBSTRING('Sakila', -5, 3);-> 'aki'
mysql> SELECT SUBSTRING('Sakila' FROM -4 FOR 2);-> 'ki'##该函数从pos参数指定位置开始向右截取 len 参数指定的字符个数(len 如果不指定,则截取到最后一个字符)。如果 pos 指定的是正数,则位置从左往右计数。如果pos指定的负数,则位置从由往左数。from 和 for 关键字可以用逗号代替

4.substring_index()

Name: 'SUBSTRING_INDEX'
Description:
Syntax:
SUBSTRING_INDEX(str,delim,count)Returns the substring from string str before count occurrences of the
delimiter delim. If count is positive, everything to the left of the
final delimiter (counting from the left) is returned. If count is
negative, everything to the right of the final delimiter (counting from
the right) is returned. SUBSTRING_INDEX() performs a case-sensitive
match when searching for delim.URL: https://dev.mysql.com/doc/refman/5.7/en/string-functions.htmlExamples:
mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);-> 'www.mysql'
mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2);-> 'mysql.com'##该函数截取指定的第几个分隔符之前的字符串,第二个参数用来指定分隔符,第三个参数指定第几个分隔符。如果count 为正数,从左往右计算分隔符个数,截取指定分隔符之前的所有字符。如果count 为负数,则从右往走计算分隔符个数,截取指定分隔符及之后的所有字符

三:MySQL 常用字符串截取操作示例

1)从第三个字符开始截取直到结束
[root@127.0.0.1][information_schema][04:42:28]> select substring('www.mysql.com',3);
+------------------------------+
| substring('www.mysql.com',3) |
+------------------------------+
| w.mysql.com                  |
+------------------------------+
1 row in set (0.00 sec)2) 从第三个字符开始截取,一共截取6个字符
[root@127.0.0.1][information_schema][04:42:38]> select substring('www.mysql.com',3,6);
+--------------------------------+
| substring('www.mysql.com',3,6) |
+--------------------------------+
| w.mysq                         |
+--------------------------------+
1 row in set (0.00 sec)3) 从倒数第三个字符开始截取,直到结束(截取的时候从左往右)
[root@127.0.0.1][information_schema][04:42:43]> select substring('www.mysql.com',-3);
+-------------------------------+
| substring('www.mysql.com',-3) |
+-------------------------------+
| com                           |
+-------------------------------+
1 row in set (0.00 sec)4) 截取第一个指定字符/字符串之前所有字符
[root@127.0.0.1][information_schema][04:43:01]> select substring_index('www.mysql.com','.',1);
+----------------------------------------+
| substring_index('www.mysql.com','.',1) |
+----------------------------------------+
| www                                    |
+----------------------------------------+
1 row in set (0.00 sec)5) 截取倒数第二个字符串之后所有的字符
[root@127.0.0.1][information_schema][04:43:30]> select substring_index('www.mysql.com','.',-2);
+-----------------------------------------+
| substring_index('www.mysql.com','.',-2) |
+-----------------------------------------+
| mysql.com                               |
+-----------------------------------------+
1 row in set (0.00 sec)6) 如果指定的分隔符不存在,则输出整个字符串
[root@127.0.0.1][information_schema][04:44:00]> select substring_index('www.mysql.com','/',-2);
+-----------------------------------------+
| substring_index('www.mysql.com','/',-2) |
+-----------------------------------------+
| www.mysql.com                           |
+-----------------------------------------+
1 row in set (0.00 sec)7)截取最后一个指定字符之前所有的字符
[root@127.0.0.1][information_schema][04:44:15]> select REVERSE(SUBSTR(REVERSE("/data/mysql/mysql3306/log/3306-bin") , INSTR(REVERSE("/data/mysql/mysql3306/log/3306-bin"),'/')+1));
+-----------------------------------------------------------------------------------------------------------------------------+
| REVERSE(SUBSTR(REVERSE("/data/mysql/mysql3306/log/3306-bin") , INSTR(REVERSE("/data/mysql/mysql3306/log/3306-bin"),'/')+1)) |
+-----------------------------------------------------------------------------------------------------------------------------+
| /data/mysql/mysql3306/log                                                                                                   |
+-----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

MySQL 字符串截取操作相关推荐

  1. Mysql字符串截取 mysql将字符串字段转为数字排序或比大小

    SELECT * FROM Student WHERE 1 = 1 ORDER BY -ID DESC ; SELECT * FROM Student WHERE 1 = 1 ORDER BY (ID ...

  2. 详解Mysql字符串截取left()、right()、substring()、substring_index()具体用法(一)

    文章目录 一.left() 二.right() 三.截取特定长度的字符串 1.从字符串第4个字符开始直结束 2.从字符串第4个字符开始,只取2个 3.从字符串倒数第4个字符开始直至结束 4.从字符串倒 ...

  3. mysql 字符串 截取字母_MySQL字符串函数:字符串截取

    MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...

  4. mysql 截取字符串部分值,Mysql字符串截取_获取指定字符串中的数据

    搜索热词 前言:本人遇到一个需求,需要在MysqL的字段中截取一段字符串中的特定字符,类似于正则表达式的截取,苦于没有合适的方法,百度之后终于找到一个合适的方法:substring_index('ww ...

  5. php mysql 截取字符串长度,mysql字符串截取

    MySQL 字符串截取函数有:left(), right(), substring(), substring_index().还有 mid(), substr(); 一:left(str, lengt ...

  6. php mysql字符串截取比较读取_MySQL字符串截取 和 截取字符进行查询

    通过mysql自带的一些字符串截取函数,对数据进行处理,下面是我整理的字符串截取 和 截取字符进行查询. 一.MySQL中字符串的截取 MySQL中有专门的字符串截取函数:其中常用的有两种:subst ...

  7. php mysql字符串截取比较读取_MySQL_Mysql字符串截取函数SUBSTRING的用法说明,感觉上MySQL的字符串函数截取 - phpStudy...

    Mysql字符串截取函数SUBSTRING的用法说明 感觉上MySQL的字符串函数截取字符,比用程序截取(如PHP或JAVA)来得强大,所以在这里做一个记录,希望对大家有用. 函数: 1.从左开始截取 ...

  8. Mysql字符串截取

    Mysql字符串截取函数:left().right().substring().substring_index(). 从左开始截取字符串: 用法:left(str, length),即:left(被截 ...

  9. MySQL 字符串截取函数,字段截取,字符串截取

    MySQL 字符串截取函数:left(), right(), substring(), substring_index().还有 mid(), substr().其中,mid(), substr() ...

最新文章

  1. MySQL 怎么变快_如何让mysql索引更快一点
  2. 算法4------字符串的字典序最长子序列
  3. java runtime 返回值_Java Runtime.exec()注意事项 | 学步园
  4. api商品分享源码_SSM框架高并发和商品秒杀项目高并发秒杀API源码免费分享
  5. QQ组件可导致IE10无响应
  6. asp.net 在线 mp3,wma, avi
  7. spring boot集成kaptcha图形验证码
  8. Robochameleon——Quick Start Guide
  9. Spark基础学习笔记17:掌握RDD算子
  10. 老年手机计算机的按键怎么调至桌面,怎样设置一键回到桌面啊,就是这个图标(如图)...
  11. 沙洋有几个微服务群_QQ群控系统强力黑科技,助力你实现自动化获客
  12. windows 8.1无人值守安装
  13. 概率论:假设检验、极大似然估计、无偏估计
  14. 超详细教程解决Win10计划任务定时重启jar服务
  15. 基于CIM的智慧城市建设
  16. 打算逃离北上广?看完这份地图大数据报告或许你有新的答案
  17. Python邮件附件保存
  18. BATH围猎新基建,后浪“TMD”集体缺席
  19. 从负债10万到存款30万:会赚钱的人都在做这件事
  20. 【JHM1400】电阻桥式或半桥式传感器信号调理模块例程

热门文章

  1. 浅读 John Backus 图灵奖获奖演讲论文
  2. 航拍深圳大运中心体育馆全景,从2018年到2021年分享
  3. 香港内推 | 香港量子人工智能实验室招聘量子化学方向研究员/高级研究员
  4. Python:函数使用
  5. 山上古树参天盘龙下载
  6. 机器学习中的高斯过程(一篇引用超20000的论文)
  7. matlab fft 频率轴,如何从fft函数求频率轴?
  8. “零糖”沦为骗局,“轻盐”却成风口?
  9. python灰色模型代码_python 实现 灰色预测 GM(1,1)模型 灰色系统 预测 灰色预测公式推导...
  10. vue微信H5(微信公众号)实现微信支付功能