如有错误,谢请指正~
概览
//连接s1,s2...sn为一个字符串
concat(s1,s2,...sn)
//将字符串str从第x位置开始,y个字符长的子串替换为字符串instr
insert(str,x,y,instr)
//将字符串str中的所有字符变为小写
lower(str)
//将字符串str中的所有字符变为大写
upper(str)
//返回字符串str最左边的x字符
left(str,x)
//返回字符串str最右边的x字符
right(str,x)
//用字符串pad对str最左边进行填充,直到长度为n个字符长度
lpad(str,n,pad)
//用字符串pad对str最右边进行填充,直到长度为n个字符长度
rpad(str,n,pad)
//去掉字符串str左侧的空格
ltrim(str)
//去掉字符串str行尾的空格
rtrim(str)
//返回str重复x次的结果
repeat(str,x)
//用字符串b替换字符串str中所有出现的字符串a
replace(str,a,b)
//比较字符串s1和s2
strcmp(s1,s2)
//去掉字符串行尾和行头的空格
trim(str)
//返回从字符串str x位置起y个字符长度的子串
substring(str,x,y)
详解
1 concat
//任何字符串与NULL进行连接的结果都将是 NULL
mysql> select concat('1','-','a'),concat('a',null);
+---------------------+------------------+
| concat('1','-','a') | concat('a',null) |
+---------------------+------------------+
| 1-a | NULL |
+---------------------+------------------+
1 row in set
2 group_concat(expr)
该函数常常和group by分组结合使用,使用逗号拼接group by出来的重复数据。
mysql> select group_concat(t.zone_name) from ts_zone t where t.zone_type = 3 ;
+----------------------------------------------------------------------------------------------+
| group_concat(t.zone_name) |
+----------------------------------------------------------------------------------------------+
| 南京市,无锡市,徐州市,常州市,苏州市,南通市,连云港市,淮安市,盐城市,扬州市,镇江市,泰州市,宿迁市 |
+----------------------------------------------------------------------------------------------+
1 row in set
mysql> select substr(t.zone_gb,1,4) as zone_gb, group_concat(t.zone_name) as zone_name from ts_zone t where (t.zone_gb like '3202%' or t.zone_gb like '3204%') and t.zone_type = 4 group by substr(t.zone_gb,1,4);
+---------+--------------------------------------------------------------------------------+
| zone_gb | zone_name |
+---------+--------------------------------------------------------------------------------+
| 3202 | 崇安区,不详县,宜兴市,江阴市,梁溪区,滨湖区,无锡新区,惠山区,锡山区,北塘区,南长区 |
| 3204 | 金坛区,武进区,新北区,戚墅堰区,钟楼区,天宁区,溧阳市 |
+---------+--------------------------------------------------------------------------------+
2 rows in set
mysql>
3 insert
insert(str,x,y,instr)函数:将字符串 str 从第x位置开始,y个字符长的子串替换为字符串 instr
mysql> select insert('java or php',6,2,'and');
+---------------------------------+
| insert('java or php',6,2,'and') |
+---------------------------------+
| java and php |
+---------------------------------+
1 row in set
4 lower(str)、upper(str)
lower(str)小写 和 upper(str)大写 函数:把字符串转换成小写或大写。
mysql> select lower('Java'),upper('java');
+---------------+---------------+
| lower('Java') | upper('java') |
+---------------+---------------+
| java | JAVA |
+---------------+---------------+
1 row in set
5 left(str,x)、right(str,x)
left(str,x) 和 right(str,x)函数:分别返回字符串最左边的x个字符和最右边的x个字符,如果第二个参数是 NULL,那么将不返回任何字符串。
mysql> select left('java and php',7),right('java and php',6);
+------------------------+-------------------------+
| left('java and php',7) | right('java and php',6) |
+------------------------+-------------------------+
| java an | nd php |
+------------------------+-------------------------+
1 row in set
mysql> select left('java and php',null),right('java and php',-1);
+---------------------------+--------------------------+
| left('java and php',null) | right('java and php',-1) |
+---------------------------+--------------------------+
| NULL | |
+---------------------------+--------------------------+
1 row in set
6 lpad(str,n,pad)、rpad(str,n,pad)
lpad(str,n,pad) 和 rpad(str,n,pad)函数:用字符串pad对str 最左边和最右边进行填充,直到长度为n个字符长度。
mysql> select lpad('java',6,'6'),rpad('java',6,'6');
+--------------------+--------------------+
| lpad('java',6,'6') | rpad('java',6,'6') |
+--------------------+--------------------+
| 66java | java66 |
+--------------------+--------------------+
1 row in set
7 trim(str)、ltrim(str)、rtrim(str)
ltrim(str)和rtrim(str)函数:去掉字符串str左侧和右侧空格
mysql> select ltrim(' java '),rtrim(' java ');
+---------------------+----------------------+
| ltrim(' java ') | rtrim(' java ') |
+---------------------+----------------------+
| java | java |
+---------------------+----------------------+
1 row in set
mysql> select trim(' java ');
+------------------+
| trim(' java ') |
+------------------+
| java |
+------------------+
1 row in set
8 repeat
repeat(str,x)函数:返回str重复x次的结果。
mysql> select repeat('java',3);
+------------------+
| repeat('java',3) |
+------------------+
| javajavajava |
+------------------+
1 row in set
9 replace(str,a,b)
replace(str,a,b)函数:用字符串b替换字符串str中所有出现的字符串a。
mysql> select replace('java','a','b');
+-------------------------+
| replace('java','a','b') |
+-------------------------+
| jbvb |
+-------------------------+
1 row in set
10 strcmp(s1,s2)
strcmp(s1,s2)函数:比较字符串ASCII码值的大小。 如果s1比s2小,那么返回-1,如果s1和s2相等,那么返回0,如果s1大于s2,那么返回1
mysql> select strcmp('a','b'),strcmp('b','b'),strcmp('c','b');
+-----------------+-----------------+-----------------+
| strcmp('a','b') | strcmp('b','b') | strcmp('c','b') |
+-----------------+-----------------+-----------------+
| -1 | 0 | 1 |
+-----------------+-----------------+-----------------+
1 row in set
11 substring(str,pos,[len])
返回从字符串str中的第pos位置起len个字符长度的字串,没有len,默认到字符串结尾。注:pos默认从1开始。
mysql> select substring('java php',1,4),substring('java php',6,3);
+---------------------------+---------------------------+
| substring('java php',1,4) | substring('java php',6,3) |
+---------------------------+---------------------------+
| java | php |
+---------------------------+---------------------------+
1 row in set
mysql> select substring('java php',6);
+-------------------------+
| substring('java php',6) |
+-------------------------+
| php |
+-------------------------+
1 row in set
12 substr(str,pos,len)
同substring
欢迎关注我的微信公众号:JAVA必知必会
- 分享精品视频资源
- 分享基础系列文档