zoukankan      html  css  js  c++  java
  • Mysql字符串截取_获取指定字符串中的数据

      前言:本人遇到一个需求,需要在MySql的字段中截取一段字符串中的特定字符,类似于正则表达式的截取,苦于没有合适的方法,百度之后终于找到一个合适的方法:substring_index('www.sqlstudy.com.cn', '.', -2)

    强烈推荐该方法获取含有特定字符的数据。

      substring_index(input,split,index):input为要截取的字符,split为分隔符,Index为要截取第index个分隔符左(index为正)或右(index为负)的字符串。

      拿个人的一个字符串举例:'Provider="RiskManagement" finalScore="65" RGID="100397278"'      我要获取finalScore的值:

    -- 1-获取finalScore右边的字符
    select substring_index('Provider="RiskManagement" finalScore="65" RGID="100397278"','finalScore="',-1);
    
    -- 2-再获取" RGID="左边的字符
    select substring_index(substring_index('Provider="RiskManagement" finalScore="65" RGID="100397278"','finalScore="',-1),'" RGID="',1);

      结果1:65" RGID="100397278"

      结果2:65

    本博客转载自:https://www.cnblogs.com/wjm956/p/7724244.html

    MySQL 字符串截取函数:left(), right(), substring(), substring_index()。还有 mid(), substr()。其中,mid(), substr() 等价于 substring() 函数,substring() 的功能非常强大和灵活。


    1. 字符串截取:left(str, length)

    mysql> select left('sqlstudy.com', 3);
    +-------------------------+
    | left('sqlstudy.com', 3) |
    +-------------------------+
    | sql |
    +-------------------------+


    2. 字符串截取:right(str, length)

    mysql> select right('sqlstudy.com', 3);
    +--------------------------+
    | right('sqlstudy.com', 3) |
    +--------------------------+
    | com |
    +--------------------------+


    3. 字符串截取:substring(str, pos); substring(str, pos, len)

      3.1 从字符串的第 4 个字符位置开始取,直到结束。

    mysql> select substring('sqlstudy.com', 4);
    +------------------------------+
    | substring('sqlstudy.com', 4) |
    +------------------------------+
    | study.com |
    +------------------------------+


      3.2 从字符串的第 4 个字符位置开始取,只取 2 个字符。

    mysql> select substring('sqlstudy.com', 4, 2);
    +---------------------------------+
    | substring('sqlstudy.com', 4, 2) |
    +---------------------------------+
    | st |
    +---------------------------------+

      3.3 从字符串的第 4 个字符位置(倒数)开始取,直到结束。

    mysql> select substring('sqlstudy.com', -4);
    +-------------------------------+
    | substring('sqlstudy.com', -4) |
    +-------------------------------+
    | .com |
    +-------------------------------+


      3.4 从字符串的第 4 个字符位置(倒数)开始取,只取 2 个字符。

    mysql> select substring('sqlstudy.com', -4, 2);
    +----------------------------------+
    | substring('sqlstudy.com', -4, 2) |
    +----------------------------------+
    | .c |
    +----------------------------------+
    我们注意到在函数 substring(str,pos, len)中, pos 可以是负值,但 len 不能取负值。

    4. 字符串截取:substring_index(str,delim,count)
      4.1 截取第二个 '.' 之前的所有字符。

    mysql> select substring_index('www.sqlstudy.com.cn', '.', 2);
    +------------------------------------------------+
    | substring_index('www.sqlstudy.com.cn', '.', 2) |
    +------------------------------------------------+
    | www.sqlstudy |
    +------------------------------------------------+
      

      4.2 截取第二个 '.' (倒数)之后的所有字符。

    mysql> select substring_index('www.sqlstudy.com.cn', '.', -2);
    +-------------------------------------------------+
    | substring_index('www.sqlstudy.com.cn', '.', -2) |
    +-------------------------------------------------+
    | com.cn |
    +-------------------------------------------------+
      

      4.3 如果在字符串中找不到 delim 参数指定的值,就返回整个字符串 

    mysql> select substring_index('www.sqlstudy.com.cn', '.coc', 1);
    +---------------------------------------------------+
    | substring_index('www.sqlstudy.com.cn', '.coc', 1) |
    +---------------------------------------------------+
    | www.sqlstudy.com.cn |
    +---------------------------------------------------+


      4.4 截取一个表某个字段数据的中间值 如该字段数据为 1,2,3

    mysql> select substring_index(substring_index(该字段, ',', 2) , ',', -1) from 表名;
    +--------------------------------------------------------------+
    | substring_index(substring_index(该字段, ',', 2); , ',', -1)|
    +--------------------------------------------------------------+
    | 2 |
    +--------------------------------------------------------------+

  • 相关阅读:
    用Photoshop设计网站的70个教程
    反射引发的错误“reflection Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.”
    机械键盘理解的几个误区和保养方法
    Asp.Net MVC中不指定View Name时如何返回ActionResult的
    [通告]Nuget服务宕机,出现 503 Server Unavailable 错误无法编译及解决方法
    MVC标签链接切换帮助类: TabLink
    [Centos 6.2] centos 6.2(64位)网络配置
    Linux(Ubuntu)设置环境变量(转载)
    COPYONWRITE 原理
    [Linux 技巧] linux screen 命令详解
  • 原文地址:https://www.cnblogs.com/lxhbky/p/11775924.html
Copyright © 2011-2022 走看看