mysql8学习笔记17--常用函数(五)
流程控制函数
• 流程控制函数包含以下四种函数
![](https://img2020.cnblogs.com/blog/947742/202007/947742-20200731071036394-1764118086.png)
• CASE value WHEN [compare_value] THEN result [WHEN
[compare_value] THEN result ...] [ELSE result] END
• 当value等于compare_value时,则返回result,否则返回else里的result,
如果没有else子句则返回null
• mysql> SELECT CASE 1 WHEN 1 THEN 'one'
• -> WHEN 2 THEN 'two' ELSE 'more' END;
• -> 'one'
• CASE WHEN [condition] THEN result [WHEN [condition] THEN
result ...] [ELSE result] END
• 当第一个condition满足时,则返回result,否则返回else里的result,如果
没有else子句时则返回null
• mysql> SELECT CASE WHEN 1>0 THEN 'true' ELSE 'false' END;
• -> 'true'
• mysql> SELECT CASE BINARY 'B'
• -> WHEN 'a' THEN 1 WHEN 'b' THEN 2 END;
• -> NULL
mysql> select * from tb_student2; +-------+--------------+--------+------------+--------------+--------+ | stuid | stuname | stusex | stubirth | stuaddr | collid | +-------+--------------+--------+------------+--------------+--------+ | 1001 | 张三 | 0 | 1990-03-04 | ABCabc | 1 | | 1002 | 季墨 | 0 | 1992-02-02 | 湖南长沙 | 1 | | 1033 | 王语嫣 | 1 | 1989-12-03 | 四川成都 | 1 | | 1378 | 纪嫣然 | 1 | 1995-08-12 | 四川绵阳 | 1 | | 1572 | 张子文 | 0 | 1993-07-19 | 陕西咸阳 | 1 | | 1954 | 张学友 | 0 | 1994-09-20 | 福建莆田 | 1 | | 2035 | 东方不败 | NULL | 1988-06-30 | NULL | 2 | | 3011 | 金大牙 | 0 | 1985-12-12 | 福建莆田 | 3 | | 3755 | 猪脚 | 0 | 1993-01-25 | NULL | 3 | | 3923 | 杨不悔 | 1 | 1985-04-17 | 四川成都 | 3 | +-------+--------------+--------+------------+--------------+--------+ 10 rows in set (0.00 sec) mysql> select stuname,case stusex -> when 0 then '男子' -> when 1 then '女子' -> else '不确定' end '性别' -> from tb_student2; +--------------+-----------+ | stuname | 性别 | +--------------+-----------+ | 张三 | 男子 | | 季墨 | 男子 | | 王语嫣 | 女子 | | 纪嫣然 | 女子 | | 张子文 | 男子 | | 张学友 | 男子 | | 东方不败 | 不确定 | | 金大牙 | 男子 | | 猪脚 | 男子 | | 杨不悔 | 女子 | +--------------+-----------+ 10 rows in set (0.00 sec) mysql>
• IF(expr1,expr2,expr3)
• 当expr1为1/true时,则返回expr2,否则返回expr3
• mysql> SELECT IF(1>2,2,3);
• -> 3
• mysql> SELECT IF(1<2,'yes','no');
• -> 'yes'
• mysql> SELECT IF(STRCMP('test','test1'),'no','yes');
• -> 'no‘
mysql> select stuname,if(stusex=0,'男子','女子') as '性别' from tb_student2; +--------------+--------+ | stuname | 性别 | +--------------+--------+ | 张三 | 男子 | | 季墨 | 男子 | | 王语嫣 | 女子 | | 纪嫣然 | 女子 | | 张子文 | 男子 | | 张学友 | 男子 | | 东方不败 | 女子 | | 金大牙 | 男子 | | 猪脚 | 男子 | | 杨不悔 | 女子 | +--------------+--------+ 10 rows in set (0.00 sec) mysql>
• IFNULL(expr1,expr2)
• 当expr1为非null时,则返回expr1,否则返回expr2
• mysql> SELECT IFNULL(1,0);
• -> 1
• mysql> SELECT IFNULL(NULL,10);
• -> 10
• mysql> SELECT IFNULL(1/0,10);
• -> 10
• mysql> SELECT IFNULL(1/0,'yes');
• -> 'yes'
mysql> select * from tb_student2; +-------+--------------+--------+------------+--------------+--------+ | stuid | stuname | stusex | stubirth | stuaddr | collid | +-------+--------------+--------+------------+--------------+--------+ | 1001 | 张三 | 0 | 1990-03-04 | ABCabc | 1 | | 1002 | 季墨 | 0 | 1992-02-02 | 湖南长沙 | 1 | | 1033 | 王语嫣 | 1 | 1989-12-03 | 四川成都 | 1 | | 1378 | 纪嫣然 | 1 | 1995-08-12 | 四川绵阳 | 1 | | 1572 | 张子文 | 0 | 1993-07-19 | 陕西咸阳 | 1 | | 1954 | 张学友 | 0 | 1994-09-20 | 福建莆田 | 1 | | 2035 | 东方不败 | NULL | 1988-06-30 | NULL | 2 | | 3011 | 金大牙 | 0 | 1985-12-12 | 福建莆田 | 3 | | 3755 | 猪脚 | 0 | 1993-01-25 | NULL | 3 | | 3923 | 杨不悔 | 1 | 1985-04-17 | 四川成都 | 3 | +-------+--------------+--------+------------+--------------+--------+ 10 rows in set (0.01 sec) mysql> select stuname,ifnull(stuaddr,'地址不详') from tb_student2; +--------------+--------------------------------+ | stuname | ifnull(stuaddr,'地址不详') | +--------------+--------------------------------+ | 张三 | ABCabc | | 季墨 | 湖南长沙 | | 王语嫣 | 四川成都 | | 纪嫣然 | 四川绵阳 | | 张子文 | 陕西咸阳 | | 张学友 | 福建莆田 | | 东方不败 | 地址不详 | | 金大牙 | 福建莆田 | | 猪脚 | 地址不详 | | 杨不悔 | 四川成都 | +--------------+--------------------------------+ 10 rows in set (0.00 sec) mysql>
• NULLIF(expr1,expr2)
• 当expr1等于expr2时,则返回null,否则返回expr1
• mysql> SELECT NULLIF(1,1);
• -> NULL
• mysql> SELECT NULLIF(1,2);
• -> 1
mysql> select nullif(2,2),nullif(2,3); +-------------+-------------+ | nullif(2,2) | nullif(2,3) | +-------------+-------------+ | NULL | 2 | +-------------+-------------+ 1 row in set (0.00 sec) mysql>
字符串函数
• ASCII(str)
• 返回str字符串中最左边字符的ascii码值,如果是空串则返回0,如果str是null则返回null
• mysql> SELECT ASCII('2');
• -> 50
• mysql> SELECT ASCII(2);
• -> 50
• mysql> SELECT ASCII('dx');
• -> 100
• CHAR(N,... [USING charset_name])
• 将括号中的N转化成ascii码对应的字符,返回这些字符组成的字符串,其中的null会被忽略
• mysql> SELECT CHAR(77,121,83,81,'76');
• -> 'MySQL'
• mysql> SELECT CHAR(77,77.3,'77.3');
• -> 'MMM‘
• mysql> SELECT CHARSET(CHAR(X'65')), CHARSET(CHAR(X'65' USING utf8));
• +----------------------+---------------------------------+
• | CHARSET(CHAR(X'65')) | CHARSET(CHAR(X'65' USING utf8)) |
• +----------------------+---------------------------------+
• | binary | utf8 |
• +----------------------+---------------------------------+
• CHAR_LENGTH(str)
• 返回字符串的字符长度
• CONCAT(str1,str2,...)
• 返回括号里所有参数字符串连接在一起,当其中有参数为NULL时则返回
NULL
• mysql> SELECT CONCAT('My', 'S', 'QL');
• -> 'MySQL'
• mysql> SELECT CONCAT('My', NULL, 'QL');
• -> NULL
• mysql> SELECT CONCAT(14.3);
• -> '14.3'
• CONCAT_WS(separator,str1,str2,...)
• 返回以第一个参数为分隔符的连接后的一个字符串,当有参数为NULL时则null
被忽略
• mysql> SELECT CONCAT_WS(',','First name','Second name','Last Name');
• -> 'First name,Second name,Last Name'
• mysql> SELECT CONCAT_WS(',','First name',NULL,'Last Name');
• -> 'First name,Last Name‘
• INSERT(str,pos,len,newstr)
• 将str中从pos位置开始后的len个字符替换成newstr字符串
• mysql> SELECT INSERT('Quadratic', 3, 4, 'What');
• -> 'QuWhattic'
• mysql> SELECT INSERT('Quadratic', -1, 4, 'What');
• -> 'Quadratic'
• mysql> SELECT INSERT('Quadratic', 3, 100, 'What');
• -> 'QuWhat'
• INSTR(str,substr)
• 返回str字符串中第一个出现substr字符串的位置
• mysql> SELECT INSTR('foobarbar', 'bar');
• -> 4
• mysql> SELECT INSTR('xbar', 'foobar');
• -> 0
• LEFT(str,len)
• 返回str字符串中从左边开始的len个长度的字符
• mysql> SELECT LEFT('foobarbar', 5);
• -> 'fooba‘
• LENGTH(str)
• 返回str字符串的byte字节长度
• mysql> SELECT LENGTH('text');
• -> 4
mysql> select concat_ws(',','First name','Second name','Last Name'),insert('Quadratic', 3, 4, 'What'),instr('foobarbar', 'bar'),left('foobarbar', 5),length('text'); +-------------------------------------------------------+-----------------------------------+---------------------------+----------------------+----------------+ | concat_ws(',','First name','Second name','Last Name') | insert('Quadratic', 3, 4, 'What') | instr('foobarbar', 'bar') | left('foobarbar', 5) | length('text') | +-------------------------------------------------------+-----------------------------------+---------------------------+----------------------+----------------+ | First name,Second name,Last Name | QuWhattic | 4 | fooba | 4 | +-------------------------------------------------------+-----------------------------------+---------------------------+----------------------+----------------+ 1 row in set (0.00 sec) mysql> select ascii('2'),ascii('23'),char(77,121,83,81,'76'),char(77,77.3,'77.3'),char_length('S'),char_length('ss'),concat('金','证','财富'); +------------+-------------+-------------------------+----------------------+------------------+-------------------+------------------------------+ | ascii('2') | ascii('23') | char(77,121,83,81,'76') | char(77,77.3,'77.3') | char_length('S') | char_length('ss') | concat('金','证','财富') | +------------+-------------+-------------------------+----------------------+------------------+-------------------+------------------------------+ | 50 | 50 | MySQL | MMM | 1 | 2 | 金证财富 | +------------+-------------+-------------------------+----------------------+------------------+-------------------+------------------------------+ 1 row in set, 1 warning (0.01 sec) mysql>
• LOAD_FILE(file_name)
• 读取文件且返回文件内容为字符串
• mysql> UPDATE t
• SET blob_col=LOAD_FILE('/tmp/picture')
• WHERE id=1;
mysql> set global secure_file_priv='/etc/my.cnf.d/';#这个参数是只读参数,可以在my.cnf里修改后重启mysql ERROR 1238 (HY000): Variable 'secure_file_priv' is a read only variable [root@localhost ~]# cat /etc/my.cnf [mysqld] secure_file_priv='' [root@localhost ~]# mysql> select load_file('/etc/my.cnf.d/mysql-clients.cnf') as result; +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | result | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | # # These groups are read by MariaDB command-line tools # Use it for options that affect only one utility # [mysql] [mysql_upgrade] [mysqladmin] [mysqlbinlog] [mysqlcheck] [mysqldump] [mysqlimport] [mysqlshow] [mysqlslap] | +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql>
• LOCATE(substr,str)
• 返回str字符串中第一次出现substr字符串的位置,如果没有则返回null
• mysql> SELECT LOCATE('bar', 'foobarbar');
• -> 4
• mysql> SELECT LOCATE('xbar', 'foobar');
• -> 0
• LOCATE(substr,str,pos)
• 返回str字符串中从pos位置开始第一次出现substr字符串的位置,如果没有则返
回null
• mysql> SELECT LOCATE('bar', 'foobarbar', 5);
• -> 7
• LOWER(str)
• 返回将str字符串中所有字符变换成小写后的字符串,但对二进制文本无
效
• mysql> SELECT LOWER('QUADRATICALLY');
• -> 'quadratically'
• mysql> SET @str = BINARY 'New York';
• mysql> SELECT LOWER(@str), LOWER(CONVERT(@str USING latin1));
• +-------------+-----------------------------------+
• | LOWER(@str) | LOWER(CONVERT(@str USING latin1)) |
• +-------------+-----------------------------------+
• | New York | new york |
• +-------------+-----------------------------------+
• LPAD(str,len,padstr)
• 用字符串 padstr对 str进行左边填补直至它的长度达到 len个字符长度,然后返回 str。如果 str的长度长于 len',那么它将被截除到 len个字符。
• mysql> SELECT LPAD('hi',4,'??');
• -> '??hi'
• mysql> SELECT LPAD('hi',1,'??');
• -> 'h‘
mysql> select lpad('CXg',1,'ab'),lpad('CXg',2,'ab'),lpad('CXg',3,'ab'),lpad('CXg',4,'ab'),lpad('CXg',5,'ab'),lpad('CXg',6,'ab'); +--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+ | lpad('CXg',1,'ab') | lpad('CXg',2,'ab') | lpad('CXg',3,'ab') | lpad('CXg',4,'ab') | lpad('CXg',5,'ab') | lpad('CXg',6,'ab') | +--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+ | C | CX | CXg | aCXg | abCXg | abaCXg | +--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+ 1 row in set (0.00 sec) mysql> select rpad('CXg',1,'ab'),rpad('CXg',2,'ab'),rpad('CXg',3,'ab'),rpad('CXg',4,'ab'),rpad('CXg',5,'ab'),rpad('CXg',6,'ab'); +--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+ | rpad('CXg',1,'ab') | rpad('CXg',2,'ab') | rpad('CXg',3,'ab') | rpad('CXg',4,'ab') | rpad('CXg',5,'ab') | rpad('CXg',6,'ab') | +--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+ | C | CX | CXg | CXga | CXgab | CXgaba | +--------------------+--------------------+--------------------+--------------------+--------------------+--------------------+ 1 row in set (0.00 sec) mysql>
• LTRIM(str)
• 将str最左边的空格去掉并返回
• mysql> SELECT LTRIM(' barbar');
• -> 'barbar‘
• REPEAT(str,count)
• 将str重复count并组合成字符串返回,如果count<1,则返回空串
• mysql> SELECT REPEAT('MySQL', 3);
• -> 'MySQLMySQLMySQL'
• REPLACE(str,from_str,to_str)
• 将所有str字符串中匹配from_str子串的地方都替换成to_str子字符串
• mysql> SELECT REPLACE('www.mysql.com', 'w', 'Ww');
• -> 'WwWwWw.mysql.com‘
• REVERSE(str)
• 将str字符串中的字符按照倒序组合并返回
• mysql> SELECT REVERSE('abc');
• -> 'cba‘
• RIGHT(str,len)
• 将str字符串中从右边开始的len个字符返回
• mysql> SELECT RIGHT('foobarbar', 4);
• -> 'rbar'
• RPAD(str,len,padstr)
• 将字符串str从右边开始补充为padstr直到整体长度为len,如果str的长度本身大
于len,则返回str中len长度的字符串
• mysql> SELECT RPAD('hi',5,'?');
• -> 'hi???'
• mysql> SELECT RPAD('hi',1,'?');
• -> 'h‘
• RTRIM(str)
• 将字符串str右边的空格去掉并返回
• mysql> SELECT RTRIM('barbar ');
• -> 'barbar‘
• SPACE(N)
• 返回N个长度的空格组成的空字符串
• mysql> SELECT SPACE(6);
• -> ' '
mysql> select locate('ABC','dddabcABC'),lower('ABCabcDDDdsc'),lpad('hi',4,'??'),ltrim(' ADDDD'),repeat('MySQL', 3),replace('bABDb','b','GG'); +---------------------------+-----------------------+-------------------+---------------------+--------------------+---------------------------+ | locate('ABC','dddabcABC') | lower('ABCabcDDDdsc') | lpad('hi',4,'??') | ltrim(' ADDDD') | repeat('MySQL', 3) | replace('bABDb','b','GG') | +---------------------------+-----------------------+-------------------+---------------------+--------------------+---------------------------+ | 4 | abcabcddddsc | ??hi | ADDDD | MySQLMySQLMySQL | GGABDGG | +---------------------------+-----------------------+-------------------+---------------------+--------------------+---------------------------+ 1 row in set (0.00 sec) mysql> select reverse('ABc'),right('foobarbar', 4),rpad('hi',5,'?'),rtrim('barbar '); +----------------+-----------------------+------------------+------------------+ | reverse('ABc') | right('foobarbar', 4) | rpad('hi',5,'?') | rtrim('barbar ') | +----------------+-----------------------+------------------+------------------+ | cBA | rbar | hi??? | barbar | +----------------+-----------------------+------------------+------------------+ 1 row in set (0.00 sec) mysql>
• SUBSTR(str,pos), SUBSTR(str FROM pos), SUBSTR(str,pos,len), SUBSTR(str FROM
pos FOR len)
• SUBSTRING(str,pos), SUBSTRING(str FROM pos), SUBSTRING(str,pos,len),
SUBSTRING(str FROM pos FOR len)
• 如果没有len参数,则返回从pos位置开始的str中的子字符串;如果有len参数,则从pos
位置开始返回str中长度为len的子字符串;如果pos为负值,则代表pos从右边开始数
• mysql> SELECT SUBSTRING('Quadratically',5);
• -> 'ratically'
• mysql> SELECT SUBSTRING('foobarbar' FROM 4);
• -> 'barbar'
• mysql> SELECT SUBSTRING('Quadratically',5,6);
• -> 'ratica'
• mysql> SELECT SUBSTRING('Sakila', -3);
• -> 'ila'
• mysql> SELECT SUBSTRING('Sakila', -5, 3);
• -> 'aki'
• mysql> SELECT SUBSTRING('Sakila' FROM -4 FOR 2);
• -> 'ki'
mysql> select substr('2020-08-02 21:29',6),substr('2020-08-02 21:29',6,5); +------------------------------+--------------------------------+ | substr('2020-08-02 21:29',6) | substr('2020-08-02 21:29',6,5) | +------------------------------+--------------------------------+ | 08-02 21:29 | 08-02 | +------------------------------+--------------------------------+ 1 row in set (0.00 sec) mysql>
• SUBSTRING_INDEX(str,delim,count)
• 当count为正数,则返回delim出现在str字符串中第count次之前的子字符
串,如果是负数,则从右边开始计算
• mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', 2);
• -> 'www.mysql'
• mysql> SELECT SUBSTRING_INDEX('www.mysql.com', '.', -2);
• -> 'mysql.com‘
• UPPER(str)
• 返回将str字符串中所有字符转换成大写的字符串
• mysql> SELECT UPPER('Hej');
• -> 'HEJ‘
字符串对比函数
![](https://img2020.cnblogs.com/blog/947742/202008/947742-20200801103708422-1941640024.png)