MySql中substring字符串截取函数用法

在mysql中常用的字符串截取函数有substring,SUBSTRING_INDEX了,substring是pos开始长度为len的字符串了,这个与php字符串截取函数有点像,下面我们一起来看看.

用法:

  1. SUBSTRING(str,pos,len)
  2. SUBSTRING(strFROMposFORlen)
  3. SUBSTRING(str,pos)
  4. SUBSTRING(strFROMpos)

别名SUBSTR,截取字符串str从pos开始长度为len的字符串,如果不设置len参数默认获取pos以后的所有内容,注意字符串的索引是从1开始,如果pos为负数则从字符串的后面开始截取.

1.截取pos后所有的数据,代码如下:

  1. mysql>selectsubstring('mysqldatabase',2);
  2. +——————————-+
  3. |substring('mysqldatabase',2)|
  4. +——————————-+
  5. |ysqldatabase|
  6. +——————————-+
  7. 1rowinset

2.截取pos后3个字符数据,代码如下:

  1. mysql>selectsubstring('mysqldatabase',2,3);
  2. +———————————+
  3. |substring('mysqldatabase',2,3)|
  4. +———————————+
  5. |ysq|
  6. +———————————+
  7. 1rowinset

3.看一些其它实例,代码如下:

  1. mysql>SELECTSUBSTRING(‘Quadratically’,5);
  2. ->‘ratically’
  3. mysql>SELECTSUBSTRING(‘foobarbar’FROM4);
  4. ->‘barbar’
  5. mysql>SELECTSUBSTRING(‘Quadratically’,5,6);
  6. ->‘ratica’
  7. mysql>SELECTSUBSTRING(‘Sakila’,-3);
  8. ->‘ila’
  9. mysql>SELECTSUBSTRING(‘Sakila’,-5,3);
  10. ->‘aki’
  11. mysql>SELECTSUBSTRING(‘Sakila’FROM-4FOR2);
  12. ->‘ki’

既然讲到这里来了我再看看:substring_index,substring_index(str,delim,count),代码如下:

  1. mysql>SELECTSUBSTRING_INDEX(‘www.phpfensi.com’,‘.’,2);
  2. ->‘www.phpfensi.com’
  3. mysql>SELECTSUBSTRING_INDEX(‘www.phpfensi.com’,‘.’,-2);
  4. ->‘phpfensi.com’

相关广告
  • MySql中substring字符串截取函数用法 MySql中substring字符串截取函数用法 MySql中substring字符串截取函数用法
相关阅读

MySql中substring字符串截取函数用法

2019/10/10 17:36:44 | 谷歌SEO算法 | SEO技术