序,做一个稍微的罗列和简单介绍,详细信息如返回值什么的还是建议去dev.mysql.com/doc来查看
一、字符串操作
0、字符串合并
concat(str1,str2, ...)
说明:将str1,str2拼接在一起,其中一个为NULL则该函数返回为NULL,故真实使用应该在外层在套上个IFULL()来处理一下
扩展:concat_ws(sep,str1,str2, ...) 可以指定每个字符串之间的分隔符
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.csdn.net', ' . ', 2) // 2的含义,关键字出现的第2次之前的子字符串返回
结果:blog.csdn
例:select substring_index ('blog.csdn.net', ' . ', -2) //-2的含义是,从右往左数第二个关键字,然后返回该关键字随后的子字符串
结果:csdn.net
(注:该函数的嵌套将可以裁剪出带分隔符的字符串中的指定的某个变量)
5、比较
Name | Description |
---|---|
LIKE | Simple pattern matching |
NOT LIKE | Negation of simple pattern matching |
STRCMP() | Compare two strings |
二、类型转化
cast(expr as type)
conver(expr,type)
convert(expr using transcoding_name) //例如:convert('中文',using gbk)
-
The
CAST()
function takes a value of one type and produce a value of another type, similar toCONVERT()
. See the description ofCONVERT()
for more information. -
CONVERT(
,expr
,type
)CONVERT(
expr
USINGtranscoding_name
)The
CONVERT()
andCAST()
functions take a value of one type and produce a value of another type.The
type
can be one of the following values:
1. IFNULL(expr1,expr2)
如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2。IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境。2. IF(expr1,expr2,expr3)
如果expr1是TRUE(expr1<>0且expr1<>NULL),那么IF()返回expr2,否则它返回expr3。IF()返回一个数字或字符串值,取决于它被使用的上下文。
3. CASE value WHEN [compare-value] THEN result [WHEN [compare-value] THEN result ...] [ELSE result] END