zoukankan      html  css  js  c++  java
  • 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                                 |
    +---------------------------------------------------+

  • 相关阅读:
    iPhone6虽好,但也要借鉴这八个功能
    中小型企业商业智能平台的开发和实现(数据仓库、BI系统、真实项目实战)
    iOS开发入门教程_iOS开发视频教程
    零基础入门jQuery视频教程
    零基础3G Android移动开发就业培训
    请求库requesets库使用
    请求头加引号工具
    请求库urllib使用
    【Liunx】saltstack运维工具
    【Liunx】消息队列rabbitmp
  • 原文地址:https://www.cnblogs.com/kenwong/p/4648315.html
Copyright © 2011-2022 走看看