计算字符串长度的函数
CHAR_LENGTH(str) 、LENGTH(str)
例1:返回字符串的字符个数,汉字和数字或字母都是1个。
1. mysql> select char_length('zhangsan'),char_length('lisi'); 2. +-------------------------+---------------------+ 3. | char_length('zhangsan') | char_length('lisi') | 4. +-------------------------+---------------------+ 5. | 8 | 4 | 6. +-------------------------+---------------------+ 7. 1 row in set (0.00 sec)
例2:返回字符串的字节长度,使用 utf8 编码字符集时,一个汉字是3个字节,一个数字或字母是1个字节
1. mysql> select length('张三'),length('zhangsan'); 2. +------------------+--------------------+ 3. | length('张三') | length('zhangsan') | 4. +------------------+--------------------+ 5. | 6 | 8 | 6. +------------------+--------------------+ 7. 1 row in set (0.00 sec)
合并字符串的函数
CONCAT(s1,s2,...) 、CONCAT_WS(x,s1,s2,...)
例1:合并多个字符串
1. mysql> select concat('北京','海淀'); 2. +---------------------------+ 3. | concat('北京','海淀') | 4. +---------------------------+ 5. | 北京海淀 | 6. +---------------------------+ 7. 1 row in set (0.01 sec)
例2:中间使用指定分隔符号,合并多个字符串
1. mysql> select concat_ws('-','北京','海淀','中关村'); 2. +----------------------------------------------+ 3. | concat_ws('-','北京','海淀','中关村') | 4. +----------------------------------------------+ 5. | 北京-海淀-中关村 | 6. +----------------------------------------------+ 7. 1 row in set (0.00 sec)
替换字符串的函数
INSERT(s1,x,len,s2)
例:从abcde的第二个字符开始,到第三个字符修改成1234
1. mysql> select insert('abcde',2,3,'1234'); 2. +----------------------------+ 3. | insert('abcde',2,3,'1234') | 4. +----------------------------+ 5. | a1234e | 6. +----------------------------+ 7. 1 row in set (0.00 sec)
转换大小写的函数
LOWER(str) 、LCASE(str) 、UPPER(str) 、UCASE(str)
例1:将大写字符串修改为小写,如果本身是小写则返回依旧是小写
1. mysql> select lower('FILE'),lower('text'); 2. +---------------+---------------+ 3. | lower('FILE') | lower('text') | 4. +---------------+---------------+ 5. | file | text | 6. +---------------+---------------+ 7. 1 row in set (0.00 sec)
例2:将大写字母转换为小写字母
1. mysql> select lcase('FILE'),lcase('TExt'); 2. +---------------+---------------+ 3. | lcase('FILE') | lcase('TExt') | 4. +---------------+---------------+ 5. | file | text | 6. +---------------+---------------+ 7. 1 row in set (0.00 sec)
例3:将小写字母转换成大写字母
1. mysql> select upper('BACKup'),upper('file'); 2. +-----------------+---------------+ 3. | upper('BACKup') | upper('file') | 4. +-----------------+---------------+ 5. | BACKUP | FILE | 6. +-----------------+---------------+ 7. 1 row in set (0.00 sec)
例4:将小写字母转换为大写字母
1. mysql> select ucase('backup'),ucase('File'); 2. +-----------------+---------------+ 3. | ucase('backup') | ucase('File') | 4. +-----------------+---------------+ 5. | BACKUP | FILE | 6. +-----------------+---------------+ 7. 1 row in set (0.00 sec)
获取指定长度的字符串的函数
LEFT(s,n) 、RIGHT(s,n)
例1:获取字符串最左侧开始的五个字符。
1. mysql> select left('abcdefg',5); 2. +-------------------+ 3. | left('abcdefg',5) | 4. +-------------------+ 5. | abcde | 6. +-------------------+ 7. 1 row in set (0.00 sec)
例2:获取字符串最右侧开始的第三个字符。
1. mysql> select right('abcdefg',3); 2. +--------------------+ 3. | right('abcdefg',3) | 4. +--------------------+ 5. | efg | 6. +--------------------+ 7. 1 row in set (0.00 sec)
填充字符串的函数
LPAD(s1,len,s2) 、RPAD(s1,len,s2)
例1:填充字符串,字符串大于4则不需要填充,直接被缩短到hell;字符串小于10则需要从左侧填充至10位,如果“s2”字符串不够,则重新排序(如:12312312)。
1. mysql> select lpad('hello',4,'123'),lpad('hello',10,'123'); 2. +-----------------------+------------------------+ 3. | lpad('hello',4,'123') | lpad('hello',10,'123') | 4. +-----------------------+------------------------+ 5. | hell | 12312hello | 6. +-----------------------+------------------------+ 7. 1 row in set (0.00 sec)
例2:填充字符串,和上方相同,只是填充方式为右侧。
1. mysql> select rpad('hello',4,'123'),rpad('hello',10,'123'); 2. +-----------------------+------------------------+ 3. | rpad('hello',4,'123') | rpad('hello',10,'123') | 4. +-----------------------+------------------------+ 5. | hell | hello12312 | 6. +-----------------------+------------------------+ 7. 1 row in set (0.00 sec)
删除空格的函数
LTRIM(s) 、RTRIM(s) 、TRIM(s)
例1:ltrim删除" hello world "左侧空格,下面可以看到仅删除了左侧空格,中间和右侧空格还在(因显示问题,可能看不出来)。
1. mysql> select ltrim( 'hello world' ); 2. +------------------------+ 3. | ltrim( 'hello world' ) | 4. +------------------------+ 5. | hello world | 6. +------------------------+ 7. 1 row in set (0.00 sec)
例2:rtrim删除" hello world "右侧空格(和ltrim命令类似,只是本次删除的右侧空格),下面可以看到仅删除了右侧空格,中间和左侧空格还在(因显示问题,可能看不出来)。
1. mysql> select rtrim( 'hello world' ); 2. +------------------------+ 3. | rtrim( 'hello world' ) | 4. +------------------------+ 5. | hello world | 6. +------------------------+ 7. 1 row in set (0.00 sec)
例3:trim删除" hello world "左右两侧空格,下面可以看到仅删除了左右侧空格,中间还在(因显示问题,可能看不出来)。
1. mysql> select trim( 'hello world' ); 2. +-----------------------+ 3. | trim( 'hello world' ) | 4. +-----------------------+ 5. | hello world | 6. +-----------------------+ 7. 1 row in set (0.00 sec)
删除指定字符串的函数
TRIM(s1 FROM s)
例:删除字符串左右两端的ab。
1. mysql> select trim('ab' from 'ab12ab3ab'); 2. +-----------------------------+ 3. | trim('ab' from 'ab12ab3ab') | 4. +-----------------------------+ 5. | 12ab3 | 6. +-----------------------------+ 7. 1 row in set (0.00 sec)
重复生成字符串的函数
REPEAT(s,n)
例1:生成三次'abc'
1. mysql> select repeat('abc',3); 2. +-----------------+ 3. | repeat('abc',3) | 4. +-----------------+ 5. | abcabcabc | 6. +-----------------+ 7. 1 row in set (0.01 sec)
例2:插入a表 ’张三‘ 循环三次
1. mysql> insert into a values(repeat('张三',3)); 2. Query OK, 1 row affected (0.05 sec) 3. 4. mysql> select * from a; 5. +--------------------+ 6. | name | 7. +--------------------+ 8. | 张三张三张三 | 9. +--------------------+ 10. 1 row in set (0.00 sec)
空格函数
SPACE(n)
例:生成10个空格
1. mysql> select space(10); 2. +------------+ 3. | space(10) | 4. +------------+ 5. | | 6. +------------+ 7. 1 row in set (0.00 sec)
替换函数
REPLACE(s,s1,s2)
例:根据内容替换字符串,但需要注意合理替换。如下所示,替换abc为全部替换,可能没有达到自己想达到的替换。
1. mysql> select replace('xxx.mysql.com','x','w'); 2. +----------------------------------+ 3. | replace('xxx.mysql.com','x','w') | 4. +----------------------------------+ 5. | www.mysql.com | 6. +----------------------------------+ 7. 1 row in set (0.01 sec) 8. 9. mysql> select replace('abc.mysql.abcd','abc','www'); 10. +----------------------------------------+ 11. | replace('abc.mysql.abcd','abc','www') | 12. +----------------------------------------+ 13. | www.mysql.wwwd | 14. +----------------------------------------+ 15. 1 row in set (0.00 sec)
比较字符串大小的函数
STRCMP(s1,s2)
例:字符串相同,返回结果为0;字符串s1大于s2,返回结果为1(这里b比a大);字符串s1小于s2,返回结果为-1
1. mysql> select strcmp('abc','abc'); 2. +---------------------+ 3. | strcmp('abc','abc') | 4. +---------------------+ 5. | 0 | 6. +---------------------+ 7. 1 row in set (0.00 sec) 8. 9. mysql> select strcmp('bbc','abc'); 10. +---------------------+ 11. | strcmp('bbc','abc') | 12. +---------------------+ 13. | 1 | 14. +---------------------+ 15. 1 row in set (0.00 sec) 16. 17. mysql> select strcmp('bbc','cbc'); 18. +---------------------+ 19. | strcmp('bbc','cbc') | 20. +---------------------+ 21. | -1 | 22. +---------------------+ 23. 1 row in set (0.00 sec)
获取子字符串的函数
SUBSTRING(s,n,len) 、MID(s,n,len)
例1:SUBSTRING(s,n,len) 用于获取指定位置的子字符串
1. mysql> SELECT SUBSTRING('breakfast',5) AS col1, # 从第5个字符串开始获取 2. -> SUBSTRING('breakfast',5,3) AS col2, # 从第5个字符串开始,获取3个 3. -> SUBSTRING('breakfast',-5) AS col3, # (倒向)从第5个字符串开始获取 4. -> SUBSTRING('breakfast',-5,3) AS col4; # (倒向)从第5个字符串开始获取,获取3个 5. +-------+------+-------+------+ 6. | col1 | col2 | col3 | col4 | 7. +-------+------+-------+------+ 8. | kfast | kfa | kfast | kfa | 9. +-------+------+-------+------+
例2:MID(s,n,len) 用于获取指定位置的子字符串
1. mysql> SELECT MID('breakfast',5) AS col1, # 从第5个字符串开始获取 2. -> MID('breakfast',5,3) AS col2, # 从第5个字符串开始,获取3个 3. -> MID('breakfast',-5) AS col3, # (倒向)从第5个字符串开始获取 4. -> MID('breakfast',-5,3) AS col4; # (倒向)从第5个字符串开始获取,获取3个 5. +-------+------+-------+------+ 6. | col1 | col2 | col3 | col4 | 7. +-------+------+-------+------+ 8. | kfast | kfa | kfast | kfa | 9. +-------+------+-------+------+
匹配子字符串开始位置的函数
LOCATE(str1,str) 、POSITION(str1 IN str) 、INSTR(str, str1)
下面三种用结果及用法相同,只有字符串位置不同。
例1:返回字符串'def'在字符串'abcdefg'中匹配到的位置,所以返回结果为4
1. mysql> select locate('def','abcdefg'); 2. +-------------------------+ 3. | locate('def','abcdefg') | 4. +-------------------------+ 5. | 4 | 6. +-------------------------+ 7. 1 row in set (0.00 sec)
例2:返回字符串'def'在字符串'abcdefg'中匹配到的位置,所以返回结果为4
1. mysql> select position('def' in 'abcdefg'); 2. +------------------------------+ 3. | position('def' in 'abcdefg') | 4. +------------------------------+ 5. | 4 | 6. +------------------------------+ 7. 1 row in set (0.00 sec)
例3:返回字符串'def'在字符串'abcdefg'中匹配到的位置,所以返回结果为4
1. mysql> select instr('abcdefg','def'); 2. +------------------------+ 3. | instr('abcdefg','def') | 4. +------------------------+ 5. | 4 | 6. +------------------------+ 7. 1 row in set (0.00 sec)
反转字符串的函数
REVERSE(s)
例:使字符串的反转
1. mysql> select reverse('abcde'); 2. +------------------+ 3. | reverse('abcde') | 4. +------------------+ 5. | edcba | 6. +------------------+ 7. 1 row in set (0.00 sec)
返回指定位置的字符串的函数
ELT(n, s1, s2, s3, .....)
例:返回第个n个字符串的值,如果n超出全部值的范围,则返回null
1. mysql> select elt(3,'a','b','c','d','e'),elt(6,'a','b','c','d','e'); 2. +----------------------------+----------------------------+ 3. | elt(3,'a','b','c','d','e') | elt(6,'a','b','c','d','e') | 4. +----------------------------+----------------------------+ 5. | c | NULL | 6. +----------------------------+----------------------------+ 7. 1 row in set (0.00 sec)
返回指定字符串位置的函数
FIELD(s, s1, s2, .....)
例:返回s在列表中的位置,如果列表中不存在则返回0
1. mysql> select field('hello','a','b','hello','d'),field('a','hello','world'); 2. +------------------------------------+----------------------------+ 3. | field('hello','a','b','hello','d') | field('a','hello','world') | 4. +------------------------------------+----------------------------+ 5. | 3 | 0 | 6. +------------------------------------+----------------------------+ 7. 1 row in set (0.00 sec)
返回子字符串位置的函数
FIND_IN_SET(s1, s2)
例:返回s1在列表中的位置,如果列表中不存在则返回0
1. mysql> select find_in_set('hello','a,b,hello,d'),find_in_set('hello','a,b,c'); 2. +------------------------------------+------------------------------+ 3. | find_in_set('hello','a,b,hello,d') | find_in_set('hello','a,b,c') | 4. +------------------------------------+------------------------------+ 5. | 3 | 0 | 6. +------------------------------------+------------------------------+ 7. 1 row in set (0.00 sec)