Home > Database > Mysql Tutorial > mysql 截取字符串

mysql 截取字符串

WBOY
Release: 2016-06-07 16:36:27
Original
1005 people have browsed it

1.字符串截取:left(str,length)mysqlselectleft('linuxidc.com',3);+-------------------------+left('linuxidc.com',3)+-------------------------+sql+-------------------------+2.字符串截取:right(str,length)mysqlselectright('linuxidc.com',3);+--

1. 字符串截取:left(str, length)  mysql> select left('linuxidc.com', 3); +-------------------------+ | left('linuxidc.com', 3) | +-------------------------+ | sql                     | +-------------------------+ 2. 字符串截取:right(str, length)  mysql> select right('linuxidc.com', 3); +--------------------------+ | right('linuxidc.com', 3) | +--------------------------+ | com                      | +--------------------------+ 3. 字符串截取:substring(str, pos); substring(str, pos, len)  3.1 从字符串的第 4 个字符位置开始取,直到结束。  mysql> select substring('linuxidc.com', 4); +------------------------------+ | substring('linuxidc.com', 4) | +------------------------------+ | study.com                    | +------------------------------+ 3.2 从字符串的第 4 个字符位置开始取,只取 2 个字符。  mysql> select substring('linuxidc.com', 4, 2); +---------------------------------+ | substring('linuxidc.com', 4, 2) | +---------------------------------+ | st                              | +---------------------------------+ 3.3 从字符串的第 4 个字符位置(倒数)开始取,直到结束。  mysql> select substring('linuxidc.com', -4); +-------------------------------+ | substring('linuxidc.com', -4) | +-------------------------------+ | .com                          | +-------------------------------+ 3.4 从字符串的第 4 个字符位置(倒数)开始取,只取 2 个字符。  mysql> select substring('linuxidc.com', -4, 2); +----------------------------------+ | substring('linuxidc.com', -4, 2) | +----------------------------------+ | .c                               | +----------------------------------+ 我们注意到在函数 substring(str,pos, len)中, pos 可以是负值,但 len 不能取负值。  4. 字符串截取:substring_index(str,delim,count)  4.1 截取第二个 '.' 之前的所有字符。  mysql> select substring_index('www.linuxidc.com', '.', 2); +------------------------------------------------+ | substring_index('www.linuxidc.com', '.', 2) | +------------------------------------------------+ | www                                  | +------------------------------------------------+ 4.2 截取第二个 '.' (倒数)之后的所有字符。  mysql> select substring_index('www.linuxidc.com', '.', -2); +-------------------------------------------------+ | substring_index('www.linuxidc.com', '.', -2) | +-------------------------------------------------+ | com.cn                                          | +-------------------------------------------------+ 4.3 如果在字符串中找不到 delim 参数指定的值,就返回整个字符串  mysql> select substring_index('www.linuxidc.com', '.coc', 1); +---------------------------------------------------+ | substring_index('www.linuxidc.com', '.coc', 1) | +---------------------------------------------------+ | www.linuxidc.com                               | +---------------------------------------------------+
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template