本文共 5063 字,大约阅读时间需要 16 分钟。
MySQL 字符串截取函数:left(), right(), substring(), substring_index()
。还有 mid(), substr()
。其中,mid(), substr() 等价于 substring() 函数,substring() 的功能非常强大和灵活。
left(str, length)
hibernate 框架不支持 mysql客户端擦查询是可以的 mysql> select left('sqlstudy.com', 3);
±------------------------+ | left(‘sqlstudy.com’, 3) | ±------------------------+ | sql | ±------------------------+ 2. 字符串截取:right(str, length)
hibernate 框架不支持 mysql客户端擦查询是可以的 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 | ±-------------------------------------------------------------+ 在 mybatis 中经常会用到大于、小于等的比较。但是,直接使用 > 、< 等符号会引起语法错误。针对这个问题,有下面两种解决办法。
原符号 | > | < | >= | <= | & | ’ | " |
---|---|---|---|---|---|---|---|
替换写法 | > | < | >= | <= | & | ' | " |
eg:select * from table where number > 50
查询表中number大于 50 的数据
CADATA代表不由 xml解析器 进行解析的文本数据。CDATA 标签的使用由 <![CDATA[
开始,由 ]]>
结束。即:<![CDATA[ sql语句 ]]>
。这里的 SQL 语句可以正常编写,大于小于等符号也可以正常使用。
eg:select * from table where <!<CDATA[ number > 50 ]]>
扩展:
mysql将浮点型转换为整型:CAST(AVG(字段名) AS SIGNED)
基本形式:属性名 regexp ‘匹配方式’
正则表达式的模式字符
^ 匹配字符开始的部分
eg1: 从info表name字段中查询以L开头的记录
select * from info where name regexp ‘^L’;
eg2: 从info表name字段中查询以aaa开头的记录
select * from info where name regexp ‘^aaa’;
$ 匹配字符结束的部分
eg1: 从info表name字段中查询以c结尾的记录
select * from info where name regexp ‘c$’;
eg2: 从info表name字段中查询以aaa结尾的记录
select * from info where name regexp ‘aaa$’;
. 匹配字符串中的任意一个字符,包括回车和换行
eg1: 从info表name字段中查询以L开头y结尾中间有两个任意字符的记录
select * from info where name regexp ‘^L…y$’;
[字符集合]匹配字符集合中的任意字符
eg1: 从info表name字段中查询包含c、e、o三个字母中任意一个的记录
select * from info where name regexp ‘[ceo]’;
eg2: 从info表name字段中查询包含数字的记录
select * from info where name regexp ‘[0-9]’;
eg3: 从info表name字段中查询包含数字或a、b、c三个字母中任意一个的记录
select * from info where name regexp ‘[0-9a-c]’;
[^字符集合]匹配除了字符集合外的任意字符
eg1: 从info表name字段中查询包含a-w字母和数字以外字符的记录
select * from info where name regexp ‘[^a-w0-9]’;
s1|s2|s3 匹配s1s2s3中的任意一个
eg1: 从info表name字段中查询包含’ic’的记录
select * from info where name regexp ‘ic’;
eg2: 从info表name字段中查询包含ic、uc、ab三个字符串中任意一个的记录
select * from info where name regexp ‘ic|uc|ab’;
* 代表多个该字符前的字符,包括0个或1个
eg1: 从info表name字段中查询c之前出现过a的记录
select * from info where name regexp ‘a*c’;
+ 代表多个该字符前的字符,包括1个
eg1: 从info表name字段中查询c之前出现过a的记录
select * from info where name regexp ‘a+c’;(注意比较结果!)
字符串{N} 字符串出现N次
eg1: 从info表name字段中查询出现过a3次的记录
select * from info where name regexp ‘a{3}’;
字符串{M,N}字符串最少出现M次,最多出现N次
eg1: 从info表name字段中查询ab出现最少1次最多3次的记录
select * from info where name regexp ‘ab{1,3}’;
MYSQL中自带通配符(LIKE关键词)
%可以表示任意长度的字符(包括0)
-可以表示单个字符
题外: 如何让# * >等符号原样显示,不被解析成对应的markdown语法,只需要在前面加上一个转义字符 "\\" 即可。
这篇文章其实是整合几篇文章内容,方便查看。
参考链接:
1、 2、 3、转载地址:http://cuncf.baihongyu.com/