php mysql 截取字符串长度_MySQL字符串截取函数

虽然在实际应用中使用程序来处理字符串截取类的操作会比使用数据库函数来实现截取的效率更好一些,但多掌握一些 MySQL 函数还是非常有必要的。这里介绍几个 MySQL 字符串截取函数,分别有:left(), right(), substring(), substring_index()。还有 mid(), substr()等。其中,mid(), substr() 等价于 substring() 函数,substring() 是一个非常强大的函数的。下面以示例分别说明每个函数的用法。

(1)left(str,length)

mysql> select left('www.phpernote.com',3);

+------------------------------+

| left('www.phpernote.com', 3) |

+------------------------------+

| www                          |

+------------------------------+

(2)right(str,length)

mysql> select right('www.phpernote.com',3);

+-------------------------------+

| right('www.phpernote.com', 3) |

+-------------------------------+

| com                           |

+-------------------------------+

(3)substring(str, pos)

mysql> select substring('www.phpernote.com', 5);

+-----------------------------------+

| substring('www.phpernote.com', 5) |

+-----------------------------------+

| phpernote.com                     |

+-----------------------------------+

mysql> select substring('www.phpernote.com', -4);//从字符串的第 4 个字符位置(倒数)开始取,直到结束

+------------------------------------+

| substring('www.phpernote.com', -4) |

+------------------------------------+

| .com                               |

+------------------------------------+

(4)substring(str, pos, len)//注意此处参数pos可取负值,但len必须是正值

mysql> select substring('www.phpernote.com', 5, 3);

+--------------------------------------+

| substring('www.phpernote.com', 5, 3) |

+--------------------------------------+

| php                                  |

+--------------------------------------+

mysql> select substring('www.phpernote.com', -4, 3);//从字符串的第 4 个字符位置(倒数)开始取,只取 3 个字符

+---------------------------------------+

| substring('www.phpernote.com', -4, 3) |

+---------------------------------------+

| .co                                   |

+---------------------------------------+

(5)substring_index(str,delim,count)

mysql> select substring_index('www.phpernote.com', '.', 2);//截取第二个 '.' 之前的所有字符

+------------------------------------------------+

| substring_index('www.phpernote.com', '.', 2)   |

+------------------------------------------------+

| www.phpernote                                  |

+------------------------------------------------+

mysql> select substring_index('www.phpernote.com', '.', -2);//截取倒数第二个 '.' 之后的所有字符

+-------------------------------------------------+

| substring_index('www.phpernote.com', '.', -2)   |

+-------------------------------------------------+

| com                                             |

+-------------------------------------------------+

mysql> select substring_index('www.phpernote.com', '.coc', 1);//如果在字符串中找不到 delim 参数指定的值,就返回整个字符串

+---------------------------------------------------+

| substring_index('www.phpernote.com', '.coc', 1)   |

+---------------------------------------------------+

| www.phpernote.com                                 |

+---------------------------------------------------+

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值