MySQL函数,是一种控制流程函数,属于数据库用语言。
MySQL数据库中提供了很丰富的函数。MySQL函数包括数学函数、字符串函数、日期和时间函数、条件判断函数、系统信息函数、加密函数、格式化函数等。通过这些函数,可以简化用户的操作。
字符串函数-函数概述
字符串函数是MySQL中常用的一类函数。主要用于处理字符串。
常用字符串函数-ascii
ASCII(str):返回字符串str的最左面字符的ASCII代码值。如果str是空字符串,返回0。如果str是NULL,返回NULL。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| mysql> select ascii('2'); + | ascii('2') | + | 50 | + 1 行于数据集 (0.01 秒)
mysql> select ascii(2); + | ascii(2) | + | 50 | + 1 行于数据集 (0.01 秒)
mysql> select ascii('Ax'); + | ascii('Ax') | + | 65 | + 1 行于数据集 (0.02 秒)
mysql> select ascii('ax'); + | ascii('ax') | + | 97 | + 1 行于数据集 (0.02 秒)
|
常用字符串函数-concat
CONCAT(str1,str2,…):返回来自于参数连结的字符串。如果任何参数是NULL,返回NULL。可以有超过2个的参数。一个数字参数被变换为等价的字符串形式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| mysql> select concat('hello','world','!'); + | concat('hello','world','!') | + | helloworld! | + 1 行于数据集 (0.02 秒)
mysql> select concat('hello',null,'world'); + | concat('hello',null,'world') | + | NULL | + 1 行于数据集 (0.04 秒)
mysql> select concat(12,21); + | concat(12,21) | + | 1221 | + 1 行于数据集 (0.02 秒)
|
常用字符串函数-length
LENGTH(str):获取字符串字节长度(返回字节数,要注意字符集)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select length('hello world'); + | length('hello world') | + | 11 | + 1 行于数据集 (0.02 秒)
mysql> select length('你好'); + | length('你好') | + | 6 | + 1 行于数据集 (0.02 秒)
|
注意:
一个汉字是算三个字节,一个数字或字母算一个字节
常用字符串函数-locate
LOCATE(substr,str):返回子串substr在字符串str第一个出现的位置,如果substr不是在str里面,返回0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select locate('wo','hello world'); + | locate('wo','hello world') | + | 7 | + 1 行于数据集 (0.04 秒)
mysql> select locate('wob','hello world'); + | locate('wob','hello world') | + | 0 | + 1 行于数据集 (0.02 秒)
|
常用字符串函数-instr
INSTR(str,substr):返回子串substr在字符串str中的第一个出现的位置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select instr('hello world','o'); + | instr('hello world','o') | + | 5 | + 1 行于数据集 (0.01 秒)
mysql> select instr('hello world','ob'); + | instr('hello world','ob') | + | 0 | + 1 行于数据集 (0.01 秒)
|
常用字符串函数-left
LEFT(str,len):返回字符串str的最左面len个字符。
1 2 3 4 5 6 7
| mysql> select left('hello world',5); + | left('hello world',5) | + | hello | + 1 行于数据集 (0.01 秒)
|
常用字符串函数-right
RIGHT(str,len):返回字符串str的最右面len个字符。
1 2 3 4 5 6 7
| mysql> select right('hello world',5); + | right('hello world',5) | + | world | + 1 行于数据集 (0.01 秒)
|
常用字符串函数-substring
SUBSTRING(str,pos):从字符串str的起始位置pos返回一个子串。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| mysql> select substring('hello world',5); + | substring('hello world',5) | + | o world | + 1 行于数据集 (0.01 秒) mysql> select substring('hello world',2,6); + | substring('hello world',2,6) | + | ello w | + 1 行于数据集 (0.01 秒)
mysql> select substring('hello world' from 7); + | substring('hello world' from 7) | + | world | + 1 行于数据集 (0.01 秒)
mysql> select substring('hello world' from 7 for 2); + | substring('hello world' from 7 for 2) | + | wo | +
mysql> select substring('hello world' from -3 for 2); + | substring('hello world' from -3 for 2) | + | rl | + 1 行于数据集 (0.01 秒)
|
常用字符串函数-trim
TRIM(str):返回字符串str,所有前缀或后缀被删除了。
1 2 3 4 5 6 7
| mysql> select trim(' hello world '); + | trim(' hello world ') | + | hello world | + 1 行于数据集 (0.01 秒)
|
常用字符串函数-ltrim/rtrim
LTRIM(str):返回删除了其前置空格字符的字符串str。
RTRIM(str):返回删除了其拖后空格字符的字符串str。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select ltrim(' hello world '); + | ltrim(' hello world ') | + | hello world | + 1 行于数据集 (0.01 秒)
mysql> select rtrim(' hello world '); + | rtrim(' hello world ') | + | hello world | + 1 行于数据集 (0.01 秒)
|
常用字符串函数-replace
REPLACE(str,from_str,to_str):返回字符串str,其字符串from_str的所有出现由字符串to_str代替。
1 2 3 4 5 6 7
| mysql> select replace('hello world','o','O'); + | replace('hello world','o','O') | + | hellO wOrld | + 1 行于数据集 (0.01 秒)
|
常用字符串函数-repeat
REPEAT(str,count):返回由重复count次的字符串str组成的一个字符串。如果count <= 0,返回一个空字符串。如果str或count是NULL,返回NULL。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select repeat('hello',3); + | repeat('hello',3) | + | hellohellohello | + 1 行于数据集 (0.01 秒)
mysql> select repeat('hello',0); + | repeat('hello',0) | + | | + 1 行于数据集 (0.01 秒)
|
常用字符串函数-reverse
REVERSE(str):返回颠倒字符顺序的字符串str。
1 2 3 4 5 6 7
| mysql> select reverse('hello world!'); + | reverse('hello world!') | + | !dlrow olleh | + 1 行于数据集 (0.02 秒)
|
常用字符串函数-insert
INSERT(str,pos,len,newstr):返回字符串str,在位置pos起始的子串且len个字符长的子串由字符串newstr代替。
1 2 3 4 5 6 7
| mysql> select insert('hello world!',5,3,'is'); + | insert('hello world!',5,3,'is') | + | hellisorld! | + 1 行于数据集 (0.02 秒)
|
常用字符串函数-elt
ETL(index,str1,str2,str3…):返回指定index位置的字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select elt(2,'hello','world','!'); + | elt(2,'hello','world','!') | + | world | + 1 行于数据集 (0.01 秒)
mysql> select elt(4,'hello','world','!'); + | elt(4,'hello','world','!') | + | NULL | + 1 行于数据集 (0.01 秒)
|
常用字符串函数-upper
UPPER(x)或UCASE(x):用于将字母转成大写,x可以是单个字母也可以是字符串;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select upper('abcdfe'); + | upper('abcdfe') | + | ABCDFE | + 1 行于数据集 (0.01 秒)
mysql> select ucase('abcdfe'); + | ucase('abcdfe') | + | ABCDFE | + 1 行于数据集 (0.01 秒)
|
常用字符串函数-lower
LOWER(x)或LCASE(x):用于将字母转成小写,x可以是单个字母也可以是字符串;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select lower('ABCDEF'); + | lower('ABCDEF') | + | abcdef | + 1 行于数据集 (0.01 秒)
mysql> select lcase('ABCDEF'); + | lcase('ABCDEF') | + | abcdef | + 1 行于数据集 (0.01 秒)
|
常用字符串函数-char_length
CHAR_LENGTH():获取字符串字符数,获取字符串长度
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select char_length('hello world'); + | char_length('hello world') | + | 11 | + 1 行于数据集 (0.01 秒)
mysql> select char_length('你好'); + | char_length('你好') | + | 2 | + 1 行于数据集 (0.01 秒)
|
注意:
不管汉字还是数字或者是字母都算是一个字符
常用字符串函数-strcmp
STRCMP(str1,str2):比较两个字符串的大小。左边大于右边时返回1,左边等于右边时返回0,,左小于于右时返回-1。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| mysql> select strcmp('a','b'); + | strcmp('a','b') | + | -1 | + 1 行于数据集 (0.01 秒)
mysql> select strcmp('d','b'); + | strcmp('d','b') | + | 1 | + 1 行于数据集 (0.01 秒)
mysql> select strcmp('b','b'); + | strcmp('b','b') | + | 0 | + 1 行于数据集 (0.01 秒)
|
常用字符串函数-field
FIELD(str,str1,str2,str3…):与find_in_set类似,返回str在str1,str2,str3…中的位置。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select field('a','b','c','d','a','e'); + | field('a','b','c','d','a','e') | + | 4 | + 1 行于数据集 (0.02 秒)
mysql> select find_in_set('a','b,c,d,a,e'); + | find_in_set('a','b,c,d,a,e') | + | 4 | + 1 行于数据集 (0.02 秒)
|
常用字符串函数-position
POSITION(str1 IN str2):返回子串str1在字符串str2中的位置
1 2 3 4 5 6 7
| mysql> select position('ld' in 'helloworld'); + | position('ld' in 'helloworld') | + | 9 | + 1 行于数据集 (0.01 秒)
|
常用字符串函数-substring_index
SUBSTRING_INDEX(str,delim,count):在定界符delim及count出现前,从字符串str返回自字符串。若count为正值,则返回最终定界符(从左边开始) ,若为-1则是从后往前截取;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| mysql> select substring_index('hello/world/!','/',-1); + | substring_index('hello/world/!','/',-1) | + | ! | + 1 行于数据集 (0.01 秒)
mysql> select substring_index('hello/world/!','/',1); + | substring_index('hello/world/!','/',1) | + | hello | + 1 行于数据集 (0.01 秒)
|