zoukankan      html  css  js  c++  java
  • mysql使用substring_index达到splite功能

    函数: 
    1、从左开始截取字符串 
    left(str, length) 
    说明:left(被截取字段,截取长度) 
    例:select left(content,200) as abstract from my_content_t 


    2、从右开始截取字符串 
    right(str, length) 
    说明:right(被截取字段,截取长度) 
    例:select right(content,200) as abstract from my_content_t 


    3、截取字符串 
    substring(str, pos) 
    substring(str, pos, length) 
    说明:substring(被截取字段,从第几位开始截取) 
    substring(被截取字段,从第几位开始截取,截取长度) 
    例:select substring(content,5) as abstract from my_content_t 
    select substring(content,5,200) as abstract from my_content_t 
    (注:如果位数是负数 如-5 则是从后倒数位数,到字符串结束或截取的长度) 


    4、按关键字截取字符串 
    substring_index(str,delim,count) 
    说明:substring_index(被截取字段,关键字,关键字出现的次数) 
    例:select substring_index("blog.jb51.net","。",2) as abstract from my_content_t 
    结果:blog.jb51 
    (注:如果关键字出现的次数是负数 如-2 则是从后倒数,到字符串结束) 

     列子:

    SELECT DISTINCT
    api.`gid`,
    api.`api_product_no`,
    api.api_name
    FROM
    area_product_info api
    WHERE api.`api_is_deleted` = 0
    AND api.api_is_public = 1
    AND api.api_status = 1
    AND api.api_is_deleted = 0
    AND api.api_mid IN
    (SELECT DISTINCT
    SUBSTRING_INDEX(
    SUBSTRING_INDEX(
    mdl.wsp_gid,
    '-',
    b.help_topic_id + 1
    ),
    ',',
    - 1
    ) wsp_gids
    FROM
    (SELECT
    awsp.wsp_has_model_info_gid wsp_gid
    FROM
    area_works_single_pano awsp
    LEFT JOIN area_member_works_info amwi
    ON amwi.`gid` = awsp.`wsp_member_works_info_gid`
    WHERE amwi.`mwi_is_delete` = 0
    AND amwi.mwi_works_no = #{worksNo}) mdl
    JOIN mysql.help_topic b
    ON b.help_topic_id < (
    LENGTH(mdl.wsp_gid) - LENGTH(REPLACE(mdl.wsp_gid, '-', ''))
    ))

    函数简介:

    SUBSTRING(str,pos) , SUBSTRING(str FROM pos) SUBSTRING(str,pos,len) , SUBSTRING(str FROM pos FOR len)

    不带有len 参数的格式从字符串str返回一个子字符串,起始于位置 pos。带有len参数的格式从字符串str返回一个长度同len字符相同的子字符串,起始于位置 pos。 使用 FROM的格式为标准 SQL 语法。也可能对pos使用一个负值。假若这样,则子字符串的位置起始于字符串结尾的pos 字符,而不是字符串的开头位置。在以下格式的函数中可以对pos 使用一个负值。

  • 相关阅读:
    vue-amap地图组件的使用
    数据分析
    爬虫之scrapy框架
    爬虫之selenium
    记录一下最近的pwn
    内核提权姿势笔记
    CVE-2016-5343分析
    CVE-2015-8660分析
    Docker远程访问get(root)shell姿势
    Linux内核ROP学习
  • 原文地址:https://www.cnblogs.com/staticed/p/8548884.html
Copyright © 2011-2022 走看看