您的位置:首页 > 数据库 > MySQL

mysql 常用的函数方法concat,substr,substring,substring_index,left,right

2017-06-22 10:39 676 查看
concat用法:

concat(str1,str2)返回字符串参数连接后的结果

eg:select concat(user_name,score) as newname from test1; ==>将user_name和score字段连接起来

substr用法:

mysql: substr(),substring()

oracle: substr()

sql server: substring()

substr(str,pos,len)//substr(str from pos for len) str从pos位置开始截取到len位置,注:pos第一个下标是1.

eg:select substr(t.user_name,1,3) as newname from test1 t;  ==》取test1中的user_name字段,从第一个字符取到第三个字符,故取前三位字符

substring_index用法:

substring_index(str,delim,count):按某些字符的下标来截取字符串
eg:select substring_index(mobile,',',1) from test1 a; ==>mobile列按‘,’取,只取1个数量.故只显示第一个逗号前的数据

left用法:

left(str,len)从左边截取len长度字符串

eg:select left(t.mobile,4) from test1 t;  ==>从mobile列中从左边取4个字符

right用法:

right(str,len)从右边截取len长度字符串

eg:select right(t.mobile,4) from test1 t;  ==>从mobile列中从右边取4个字符
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: