• MySQL字符串提取


    MySQL字符串切分有两种方式,一个是substring函数,一个是substring_index函数,下面来介绍一下两种切分的用法

    SUBSTRING函数

    SUBSTRING函数从特定位置开始的字符串返回一个给定长度的子字符串。 MySQL提供了各种形式的子串功能。
    在这里插入图片描述

    SUBSTRING(string, position, length);
    string: 需要提取字符串的字符串
    position: 参数是一个整数,用于指定子串的起始字符,position可以是整或负数
    length: 提取字符串的长度

    从第7位开始提取字符串

    mysql> SELECT SUBSTRING('MySQL SUBSTRING',7);
    +----------------------------------+
    | SUBSTRING('MySQL SUBSTRING',7)   |
    +----------------------------------+
    | SUBSTRING                        |
    +----------------------------------+
    1 row in set
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    从第1个字符开始提取,提取5个字符串

    mysql> SELECT SUBSTRING('MySQL SUBSTRING',1,5);
    +----------------------------------+
    | SUBSTRING('MySQL SUBSTRING',1,5) |
    +----------------------------------+
    | MySQL                            |
    +----------------------------------+
    1 row in set
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    另一种写法

    SUBSTRING(string FROM position for length);
    string: 要提取字符串的字符串
    position: 参数是一个整数,用于指定子串的起始字符,position可以是正或负整数
    length: 提取字符串的长度

    mysql> SELECT SUBSTRING('MySQL SUBSTRING' FROM 1 FOR 5);
    +-------------------------------------------+
    | SUBSTRING('MySQL SUBSTRING' FROM 1 FOR 5) |
    +-------------------------------------------+
    | MySQL                                     |
    +-------------------------------------------+
    1 row in set
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    position为负数的时候,表示从后往前数

    mysql> SELECT SUBSTRING('MySQL SUBSTRING' FROM -15 FOR 5);
    +---------------------------------------------+
    | SUBSTRING('MySQL SUBSTRING' FROM -15 FOR 5) |
    +---------------------------------------------+
    | MySQL                                       |
    +---------------------------------------------+
    1 row in set
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如果positionlength的总和大于字符串的字符数,则SUBSTRING函数将返回一个从位置开始到字符串末尾的子串。

    mysql> select substring('MySQL substring', 3,19);
    +----------------------------------+
    | SUBSTRING('MySQL SUBSTRING',1,5) |
    +----------------------------------+
    | SQL substring                    |
    +----------------------------------+
    1 row in set
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    SUBSTRING_INDEX函数

    SUBSTRING-_INDEX(str, delim, count);
    string: 要提取字符串的字符串
    delim: 分隔符
    count: 当 count 为正数,取第 n 个分隔符之前的所有字符; 当 count 为负数,取倒数第 n 个分隔符之后的所有字符。

    mysql> select substring_index('www.MySQL.substringIndex.com','.',2);
    +-------------------------------------------------------+
    | substring_index('www.MySQL.substringIndex.com','.',2) |
    +-------------------------------------------------------+
    | www.MySQL                                             |
    +-------------------------------------------------------+
    1 row in set
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    mysql> select substring_index('www.MySQL.substringIndex.com','.',-2);
    +----------------------------------------------------- --+
    | substring_index('www.MySQL.substringIndex.com','.',-2) |
    +------------------------------------------------------ -+
    | substringIndex.com                                     |
    +------------------------------------------------------- +
    1 row in set
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如果想抽取中间一部分,可以嵌套使用

    mysql> select substring_index(substring_index('www.MySQL.substringIndex.com','.',3),'.', -2);
    +--------------------------------------------------------------------------------+
    | substring_index(substring_index('www.MySQL.substringIndex.com','.',3),'.', -2) |
    +--------------------------------------------------------------------------------+
    | MySQL.substringIndex                                                             |
    +--------------------------------------------------------------------------------+
    1 row in set
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    Springboot全局异常和自定义异常
    Electron和vue3集成(可用于生产打包)
    Android手势识别类,GestureDetector,ScaleGestureDetector
    Fastjson_1.2.24_unserialize_rce漏洞复现
    RN开发搬砖经验之-如何处理FlashList组件加载后调用scrollToIndex没有滚动指定位置
    深度学习(CNN+RNN)笔记2
    「数据结构详解·一」树的初步
    简单了解Vue及其指令
    C语言 IO函数练习
    MySql 数据库初始化,创建用户,创建数据库,授权
  • 原文地址:https://blog.csdn.net/weixin_46599926/article/details/126374341