zoukankan      html  css  js  c++  java
  • MySQL——截取字符串函数的sql语句

    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                      |  

    #查询某个字段后两位字符
    select right(last3, 2) as last2 from historydata limit 10;
    #从应该字段取后两位字符更新到另外一个字段
    update `historydata` set `last2`=right(last3, 2);


    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                                        |   

     

    我最常用:想要得到 wsProduct:true,如下:

    select substring_index(substring_index('pay_discount:0;priceExchange:2650.00000000;shippingfeee_pwner:buyer;wsId:179356;wsProduct:true;wsProductDiscount:-5300;wsProductPrice:3957', 'wsProduct', 2) , ';', -2)

     

    参考链接:https://www.cnblogs.com/wjm956/p/7724244.html

     

    爱生活,更爱给我带来生活的人
  • 相关阅读:
    call/cc 总结 | Scheme
    用call/cc合成所有的控制流结构
    词法作用域 vs 动态作用域
    数论部分第二节:埃拉托斯特尼筛法
    1022: [SHOI2008]小约翰的游戏John【Nim博弈,新生必做的水题】
    C++面向对象作业1
    数论部分第一节:素数与素性测试【详解】
    基数排序与桶排序,计数排序【详解】
    计蒜客:百度的科学计算器(简单)【python神解】
    优质免费在线学习网站【自用】
  • 原文地址:https://www.cnblogs.com/chenyq/p/14784852.html
Copyright © 2011-2022 走看看