MySQL的常用函数
一、条件判断函数
# 1. if(表达式,值1,值2):若表达式成立(为true)则返回值1,否则返回值2
mysql> select if(0=false,'true','false');
+----------------------------+
| if(0=false,'true','false') |
+----------------------------+
| true |
+----------------------------+
1 row in set (0.00 sec)
mysql> select if(2=true,'true','false');
+---------------------------+
| if(2=true,'true','false') |
+---------------------------+
| false |
+---------------------------+
1 row in set (0.00 sec)
mysql> select if(1=true,'true','false');
+---------------------------+
| if(1=true,'true','false') |
+---------------------------+
| true |
+---------------------------+
1 row in set (0.00 sec)
+----------------------------------+
| mid('我们都有一个家',1,2) |
+----------------------------------+
| 我们 |
+----------------------------------+
1 row in set (0.00 sec)
# 2. ifnull(value1,value2):若值1的值为null,则返回value2,否则返回value1
mysql> select ifnull(null,'null');
+---------------------+
| ifnull(null,'null') |
+---------------------+
| null |
+---------------------+
1 row in set (0.00 sec)
mysql> select ifnull('a',null);
+------------------+
| ifnull('a',null) |
+------------------+
| a |
+------------------+
1 row in set (0.00 sec)
# 3. case expr when val1 then expr1 [when val2 then expr2 ...] [else default] end
如果表达式expr等于某个valn的值,则返回对应位置的then后面的exprn,若都不等于,则返回else后面的default默认值
mysql> select case 5 when 2 then 'a' when 5 then 'b' else 'c' end;
+-----------------------------------------------------+
| case 5 when 2 then 'a' when 5 then 'b' else 'c' end |
+-----------------------------------------------------+
| b |
+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql> select case 0 when 2 then 'a' when 5 then 'b' else 'c' end;
+-----------------------------------------------------+
| case 0 when 2 then 'a' when 5 then 'b' else 'c' end |
+-----------------------------------------------------+
| c |
+-----------------------------------------------------+
1 row in set (0.00 sec)
mysql> select case (select gender from students where id=1) when 1 then '男生' when 2 then '女生' else '未知' end;
+-----------------------------------------------------------------------------------------------------------+
| case (select gender from students where id=1) when 1 then '男生' when 2 then '女生' else '未知' end |
+-----------------------------------------------------------------------------------------------------------+
| 男生 |
+-----------------------------------------------------------------------------------------------------------+
1 row in set (0.03 sec)
mysql> select name,gender,case gender when 1 then '男生' when 2 then '女生' else '未知' end 性别 from students where id=1;
+-----------+--------+--------+
| name | gender | 性别 |
+-----------+--------+--------+
| 包不同 | 1 | 男生 |
+-----------+--------+--------+
1 row in set (0.00 sec)
mysql> select name,score,case when score>=90 then '良好' when score>=80 then '优秀' when score>=60 then '及格' else '不及格' end 成绩 from students where id=1;
+-----------+-------+--------+
| name | score | 成绩 |
+-----------+-------+--------+
| 包不同 | 87 | 优秀 |
+-----------+-------+--------+
1 row in set (0.00 sec)
二、字符函数
# 1. concat(字符串1,字符串2,...):拼接多个字符串
mysql> select concat('a','b','c');
+---------------------+
| concat('a','b','c') |
+---------------------+
| abc |
+---------------------+
1 row in set (0.00 sec)
# 2. concat_ws(x,string,string):用x拼接字符串
mysql> select concat_ws('、','a','b','c','d');
+----------------------------------+
| concat_ws('、','a','b','c','d') |
+----------------------------------+
| a、b、c、d |
+----------------------------------+
1 row in set (0.00 sec)
# 3. length(string):获取字符串字节长度
mysql> select length('abcd');
+----------------+
| length('abcd') |
+----------------+
| 4 |
+----------------+
1 row in set (0.00 sec)
mysql> select length('我');
+---------------+
| length('我') |
+---------------+
| 3 |
+---------------+
1 row in set (0.00 sec)
# 4. char_length(string):获取字符串字符长度
mysql> select char_length('我');
+--------------------+
| char_length('我') |
+--------------------+
| 1 |
+--------------------+
1 row in set (0.00 sec)
mysql> select char_length('abcd');
+---------------------+
| char_length('abcd') |
+---------------------+
| 4 |
+---------------------+
1 row in set (0.00 sec)
# 5. substring(string,开始索引(从1开始)/mid(string,开始索引(从1开始),截取的字符长度):截取字符串
mysql> select substring('abcdefg',0);
+------------------------+
| substring('abcdefg',0) |
+------------------------+
| |
+------------------------+
1 row in set (0.00 sec)
mysql> select substring('abcdefg',1);
+------------------------+
| substring('abcdefg',1) |
+------------------------+
| abcdefg |
+------------------------+
1 row in set (0.00 sec)
mysql> select substring('abcdefg',1,2);
+--------------------------+
| substring('abcdefg',1,2) |
+--------------------------+
| ab |
+--------------------------+
1 row in set (0.00 sec)
mysql> select substring('我们都有一个家',1,2);
+----------------------------------------+
| substring('我们都有一个家',1,2) |
+----------------------------------------+
| 我们 |
+----------------------------------------+
1 row in set (0.00 sec)
# 6. instr(string1,string2):查找string2在string1中第一次出现的索引
mysql> select instr('aaskjchaidquiw','a');
+-----------------------------+
| instr('aaskjchaidquiw','a') |
+-----------------------------+
| 1 |
+-----------------------------+
1 row in set (0.00 sec)
mysql> select instr('aaskjchaidquiw','b');
+-----------------------------+
| instr('aaskjchaidquiw','c') |
+-----------------------------+
| 6 |
+-----------------------------+
1 row in set (0.00 sec)
# 7. trim():去掉字符串前后指定的字符,默认去掉空格
mysql> select trim(' a s a ');
+---------------------+
| trim(' a s a ') |
+---------------------+
| a s a |
+---------------------+
1 row in set (0.00 sec)
mysql> select trim('@' from '@@@@@@ass@@dadkj@@@');
+--------------------------------------+
| trim('@' from '@@@@@@ass@@dadkj@@@') |
+--------------------------------------+
| ass@@dadkj |
+--------------------------------------+
1 row in set (0.00 sec)
# 8. ltrim()/rtrim():删除字符串左/右边的空格
mysql> select ltrim(' sdsd '),length(ltrim(' sdsd '));
+-----------------+-------------------------+
| ltrim(' sdsd ') | length(ltrim(' sdsd ')) |
+-----------------+-------------------------+
| sdsd | 5 |
+-----------------+-------------------------+
1 row in set (0.00 sec)
mysql> select rtrim(' sdsd '),length(rtrim(' sdsd '));
+-----------------+-------------------------+
| rtrim(' sdsd ') | length(rtrim(' sdsd ')) |
+-----------------+-------------------------+
| sdsd | 5 |
+-----------------+-------------------------+
1 row in set (0.00 sec)
# 9. lpad()/rpad():左填充/右填充
mysql> select lpad('段誉',8,'#');
+----------------------+
| lpad('段誉',8,'#') |
+----------------------+
| ######段誉 |
+----------------------+
1 row in set (0.00 sec)
mysql> select rpad('段誉',8,'#');
+----------------------+
| rpad('段誉',8,'#') |
+----------------------+
| 段誉###### |
+----------------------+
1 row in set (0.00 sec)
# 10. (upper()/ucase())/(lower()/lcase()):将字符串转为大/小写
mysql> select upper('abcd');
+---------------+
| upper('abcd') |
+---------------+
| ABCD |
+---------------+
1 row in set (0.00 sec)
mysql> select lower('ABcd');
+---------------+
| lower('ABcd') |
+---------------+
| abcd |
+---------------+
1 row in set (0.00 sec)
mysql> select ucase('abcd');
+---------------+
| ucase('abcd') |
+---------------+
| ABCD |
+---------------+
1 row in set (0.00 sec)
mysql> select lcase('ABCD');
+---------------+
| lcase('ABCD') |
+---------------+
| abcd |
+---------------+
1 row in set (0.00 sec)
# 11. strcmp():比较两个字符串的大小
mysql> select strcmp('abcd','aaaa');
+-----------------------+
| strcmp('abcd','aaaa') |
+-----------------------+
| 1 |
+-----------------------+
1 row in set (0.00 sec)
mysql> select strcmp('abcd','acd');
+----------------------+
| strcmp('abcd','acd') |
+----------------------+
| -1 |
+----------------------+
1 row in set (0.00 sec)
mysql> select strcmp('abcd','abcd');
+-----------------------+
| strcmp('abcd','abcd') |
+-----------------------+
| 0 |
+-----------------------+
# 12. left(string,n)/right(string,n):返回从最左/右边开始的n个字符
mysql> select left('abcdefg',2);
+-------------------+
| left('abcdefg',2) |
+-------------------+
| ab |
+-------------------+
1 row in set (0.00 sec)
mysql> select right('abcdefg',2);
+--------------------+
| right('abcdefg',2) |
+--------------------+
| fg |
+--------------------+
1 row in set (0.00 sec)
# 13. repeat(string,n):返回重复n次的string字符串
mysql> select repeat('ab',4);
+----------------+
| repeat('ab',4) |
+----------------+
| abababab |
+----------------+
# 14. space(n):返回由n个空格组成的字符串
mysql> select space(5),length(space(5));
+----------+------------------+
| space(5) | length(space(5)) |
+----------+------------------+
| | 5 |
+----------+------------------+
1 row in set (0.00 sec)
# 15. replace(string,s1,s2):返回一个用s2替换string中的s1的字符串
mysql> select replace('jalscsjvbsdjvsd','s','D');
+------------------------------------+
| replace('jalscsjvbsdjvsd','s','D') |
+------------------------------------+
| jalDcDjvbDdjvDd |
+------------------------------------+
1 row in set (0.00 sec)
# 16. locate(str,str1):返回字符串str在str1中第一次出现的位置索引
mysql> select locate('d','ascddev');
+-----------------------+
| locate('d','ascddev') |
+-----------------------+
| 4 |
+-----------------------+
1 row in set (0.00 sec)
# 17. position(str in str1):返回字符串str在str1中第一次出现的位置索引
mysql> select position('d' in 'asdsajhk');
+-----------------------------+
| position('d' in 'asdsajhk') |
+-----------------------------+
| 3 |
+-----------------------------+
1 row in set (0.00 sec)
# 18. reverse(str):反转字符串
mysql> select reverse('abcdefg');
+--------------------+
| reverse('abcdefg') |
+--------------------+
| gfedcba |
+--------------------+
1 row in set (0.00 sec)
# 19. elt(n,str1,str2,str3,...):返回第n个字符串
mysql> select elt(2,'a','b','c','d');
+------------------------+
| elt(2,'a','b','c','d') |
+------------------------+
| b |
+------------------------+
1 row in set (0.00 sec)
mysql> select elt(0,'a','b','c','d');
+------------------------+
| elt(0,'a','b','c','d') |
+------------------------+
| NULL |
+------------------------+
1 row in set (0.00 sec)
mysql> select elt(8,'a','b','c','d');
+------------------------+
| elt(8,'a','b','c','d') |
+------------------------+
| NULL |
+------------------------+
1 row in set (0.00 sec)
三、数学函数
# 1. abs(n):返回n的绝对值
mysql> select abs(-3);
+---------+
| abs(-3) |
+---------+
| 3 |
+---------+
1 row in set (0.00 sec)
mysql> select abs(3);
+--------+
| abs(3) |
+--------+
| 3 |
+--------+
1 row in set (0.00 sec)
mysql> select abs(0);
+--------+
| abs(0) |
+--------+
| 0 |
+--------+
1 row in set (0.00 sec)
# 2. PI():返回圆周率,默认返回6位小数
mysql> select pi();
+----------+
| pi() |
+----------+
| 3.141593 |
+----------+
1 row in set (0.00 sec)
# 3. sqrt(n):返回非负数的二次方根,n为负数是返回null
mysql> select sqrt(16.9);
+-------------------+
| sqrt(16.9) |
+-------------------+
| 4.110960958218893 |
+-------------------+
1 row in set (0.00 sec)
mysql> select sqrt(-16);
+-----------+
| sqrt(-16) |
+-----------+
| NULL |
+-----------+
1 row in set (0.00 sec)
# 4. mod(x,y):返回x被y除后的余数
mysql> select mod(7,5);
+----------+
| mod(7,5) |
+----------+
| 2 |
+----------+
1 row in set (0.00 sec)
# 5. ceil(n)/ceiling(n):返回不小于n的最小整数(向上取整)
mysql> select ceil(0.2);
+-----------+
| ceil(0.2) |
+-----------+
| 1 |
+-----------+
1 row in set (0.00 sec)
mysql> select ceiling(0.2);
+--------------+
| ceiling(0.2) |
+--------------+
| 1 |
+--------------+
1 row in set (0.00 sec)
mysql> select ceiling(-0.2);
+---------------+
| ceiling(-0.2) |
+---------------+
| 0 |
+---------------+
1 row in set (0.00 sec)
mysql> select ceil(-0.2);
+------------+
| ceil(-0.2) |
+------------+
| 0 |
+------------+
1 row in set (0.00 sec)
# 6. floor(n):返回不大于n的最大整数(向下取整)
mysql> select floor(1.0032);
+---------------+
| floor(1.0032) |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)
# 7. round(n[,x]):返回n的四舍五入值,x为保留的小数点位数,不传x时保留四舍五入的整数
mysql> select round(3.456,1);
+----------------+
| round(3.456,1) |
+----------------+
| 3.5 |
+----------------+
1 row in set (0.00 sec)
mysql> select round(3.456);
+--------------+
| round(3.456) |
+--------------+
| 3 |
+--------------+
1 row in set (0.00 sec)
# 8. sign(n):返回n的正负号,-1表示负数,0表示0,1表示正数
mysql> select sign(-3);
+----------+
| sign(-3) |
+----------+
| -1 |
+----------+
1 row in set (0.05 sec)
mysql> select sign(0);
+---------+
| sign(0) |
+---------+
| 0 |
+---------+
1 row in set (0.00 sec)
mysql> select sign(3);
+---------+
| sign(3) |
+---------+
| 1 |
+---------+
1 row in set (0.00 sec)
# 10. pow(n,x)/power(n,x):返回n的x次方
mysql> select pow(4,2);
+----------+
| pow(4,2) |
+----------+
| 16 |
+----------+
1 row in set (0.00 sec)
mysql> select power(4,2);
+------------+
| power(4,2) |
+------------+
| 16 |
+------------+
1 row in set (0.00 sec)
# 11. exp(n):返回e的n乘方值
mysql> select exp(2);
+------------------+
| exp(2) |
+------------------+
| 7.38905609893065 |
+------------------+
1 row in set (0.00 sec)
mysql> select exp(1);
+-------------------+
| exp(1) |
+-------------------+
| 2.718281828459045 |
+-------------------+
1 row in set (0.00 sec)
# 12. log(n):返回n的自然对数,n相对于基数e的对数
mysql> select log(20);
+-------------------+
| log(20) |
+-------------------+
| 2.995732273553991 |
+-------------------+
1 row in set (0.00 sec)
# 13. radians(n):返回由n角度转化为弧度的值
mysql> select radians(30);
+--------------------+
| radians(30) |
+--------------------+
| 0.5235987755982988 |
+--------------------+
1 row in set (0.00 sec)
# 14. degrees(n):返回由n弧度转化为角度的值
mysql> select degrees(0.6);
+-------------------+
| degrees(0.6) |
+-------------------+
| 34.37746770784939 |
+-------------------+
1 row in set (0.00 sec)
# 15. sin(n)/asin(n):sin(n)返回n的正弦值,n为弧度值;asin(n)返回n的反正弦值,n为正弦
mysql> select sin(0.5);
+-------------------+
| sin(0.5) |
+-------------------+
| 0.479425538604203 |
+-------------------+
1 row in set (0.00 sec)
mysql> select asin(0.5);
+--------------------+
| asin(0.5) |
+--------------------+
| 0.5235987755982989 |
+--------------------+
1 row in set (0.00 sec)
# 16. cos(n)/acos(n):cos(n)返回n的余弦值,n为弧度值;acos(n)返回n的反余弦值,n为余弦
mysql> select cos(0.1);
+--------------------+
| cos(0.1) |
+--------------------+
| 0.9950041652780258 |
+--------------------+
1 row in set (0.00 sec)
mysql> select acos(0.1);
+--------------------+
| acos(0.1) |
+--------------------+
| 1.4706289056333368 |
+--------------------+
1 row in set (0.00 sec)
# 17. tan(n)/atan(n):返回n的正切值,n为弧度值;atan(n)返回n的反正切值,n为正切
mysql> select tan(0.5);
+--------------------+
| tan(0.5) |
+--------------------+
| 0.5463024898437905 |
+--------------------+
1 row in set (0.00 sec)
mysql> select atan(0.5);
+--------------------+
| atan(0.5) |
+--------------------+
| 0.4636476090008061 |
+--------------------+
1 row in set (0.00 sec)
# 18. cot(n):返回弧度n的余切
mysql> select cot(0.5);
+-------------------+
| cot(0.5) |
+-------------------+
| 1.830487721712452 |
+-------------------+
1 row in set (0.01 sec)
# 19. rand():返回0~1之间的随机数
mysql> select rand();
+---------------------+
| rand() |
+---------------------+
| 0.18810265342528248 |
+---------------------+
1 row in set (0.00 sec)
四、日期和时间函数
# 1. curdate()/current_date():按照'YYYY-MM-DD'或者'YYYYMMDD'格式的日期返回,具体格式看语境是字符串还是数字
mysql> select curdate();
+------------+
| curdate() |
+------------+
| 2020-11-17 |
+------------+
1 row in set (0.00 sec)
mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2020-11-17 |
+----------------+
1 row in set (0.00 sec)
mysql> select current_date()+1;
+------------------+
| current_date()+1 |
+------------------+
| 20201118 |
+------------------+
1 row in set (0.00 sec)
# 2. now()/localtime()/sysdate()/current_timestamp():以'YYYY-MM-DD HH:MM:SS'或'YYYYMMDDHHMMSS'格式返回当前时间,具体看语境是字符串还是数字
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2020-11-17 10:28:05 |
+---------------------+
1 row in set (0.00 sec)
mysql> select localtime();
+---------------------+
| localtime() |
+---------------------+
| 2020-11-17 10:28:13 |
+---------------------+
1 row in set (0.00 sec)
mysql> select sysdate();
+---------------------+
| sysdate() |
+---------------------+
| 2020-11-17 10:28:22 |
+---------------------+
1 row in set (0.00 sec)
mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2020-11-17 10:28:38 |
+---------------------+
1 row in set (0.00 sec)
mysql> select current_timestamp()+1;
+-----------------------+
| current_timestamp()+1 |
+-----------------------+
| 20201117102908 |
+-----------------------+
1 row in set (0.00 sec)
# 3. unix_timestamp([date/datetime]):返回给定时间的时间戳
mysql> select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
| 1605580405 |
+------------------+
1 row in set (0.00 sec)
mysql> select unix_timestamp('2020-11-17 10:33:30');
+---------------------------------------+
| unix_timestamp('2020-11-17 10:33:30') |
+---------------------------------------+
| 1605580410 |
+---------------------------------------+
1 row in set (0.00 sec)
mysql> select unix_timestamp('2020-11-17');
+------------------------------+
| unix_timestamp('2020-11-17') |
+------------------------------+
| 1605542400 |
+------------------------------+
1 row in set (0.00 sec)
# 4. from_unixtime(timestamp):unix_timestamp()的反函数,将时间戳转换为'YYYY-MM-DD HH:MM:SS.毫秒'
mysql> select from_unixtime('1605542400');
+-----------------------------+
| from_unixtime('1605542400') |
+-----------------------------+
| 2020-11-17 00:00:00.000000 |
+-----------------------------+
1 row in set (0.00 sec)
# 5. utc_date():返回当前utc(世界标准时间)日期值,格式'YYYY-MM-DD'或'YYYYMMDD',具体格式看语境
mysql> select utc_date();
+------------+
| utc_date() |
+------------+
| 2020-11-17 |
+------------+
1 row in set (0.00 sec)
mysql> select utc_date()+0;
+--------------+
| utc_date()+0 |
+--------------+
| 20201117 |
+--------------+
1 row in set (0.00 sec)
# 6. utc_time():返回当前utc(世界标准时间)时间值,格式'HH:MM:SS'或'HHMMSS',具体格式看语境
mysql> select utc_time();
+------------+
| utc_time() |
+------------+
| 02:51:01 |
+------------+
1 row in set (0.00 sec)
mysql> select utc_time()+0;
+--------------+
| utc_time()+0 |
+--------------+
| 25103 |
+--------------+
1 row in set (0.00 sec)
# 7. month(date/datetime):返回指定日期的月份
mysql> select month(now());
+--------------+
| month(now()) |
+--------------+
| 11 |
+--------------+
1 row in set (0.00 sec)
mysql> select month(curdate());
+------------------+
| month(curdate()) |
+------------------+
| 11 |
+------------------+
1 row in set (0.00 sec)
# 8. monthname(date/datetime):返回指定日期的月份英文名称
mysql> select monthname(now());
+------------------+
| monthname(now()) |
+------------------+
| November |
+------------------+
1 row in set (0.00 sec)
mysql> select monthname(curdate());
+----------------------+
| monthname(curdate()) |
+----------------------+
| November |
+----------------------+
1 row in set (0.00 sec)
# 9. dayname(date/datetime):返回周几的英文名
mysql> select dayname(now());
+----------------+
| dayname(now()) |
+----------------+
| Tuesday |
+----------------+
1 row in set (0.00 sec)
mysql> select dayname(utc_date());
+---------------------+
| dayname(utc_date()) |
+---------------------+
| Tuesday |
+---------------------+
1 row in set (0.00 sec)
# 10. dayofweek(date/datetime):返回给定的日期是一周中的第几天(周日是第一天,值为1)
mysql> select dayofweek(now());
+------------------+
| dayofweek(now()) |
+------------------+
| 3 |
+------------------+
1 row in set (0.00 sec)
mysql> select dayofweek(utc_date());
+-----------------------+
| dayofweek(utc_date()) |
+-----------------------+
| 3 |
+-----------------------+
1 row in set (0.00 sec)
# 11. weekday(date/datetime):返回给定的日期是一周中的第几天(周日是第一天,值为0)
mysql> select weekday(utc_date());
+---------------------+
| weekday(utc_date()) |
+---------------------+
| 1 |
+---------------------+
1 row in set (0.00 sec)
# 12. week(date[,mode]):返回一年中的第几周
mysql> select week(now()),week(now(),3);
+-------------+---------------+
| week(now()) | week(now(),3) |
+-------------+---------------+
| 46 | 47 |
+-------------+---------------+
1 row in set (0.01 sec)
# 13. weekofyear(date):返回日期的日历周(1-53)和week(date,3)相同
mysql> select weekofyear(now());
+-------------------+
| weekofyear(now()) |
+-------------------+
| 47 |
+-------------------+
1 row in set (0.00 sec)
# 14. dayofyear(date)/dayofmonth(date):返回一年/一个月中的第几天
mysql> select dayofyear(now()),dayofmonth(now());
+------------------+-------------------+
| dayofyear(now()) | dayofmonth(now()) |
+------------------+-------------------+
| 322 | 17 |
+------------------+-------------------+
1 row in set (0.00 sec)
# 15. year(date):返回指定日期的年份;quarter(date):返回一年中的第几季度(1~4);minute(time):返回给定时间的分钟数(0~59);second(time):返回给定时间的秒数(0~59)
mysql> select year(now()),quarter(now()),minute(utc_time()),second(utc_time());
+-------------+----------------+--------------------+--------------------+
| year(now()) | quarter(now()) | minute(utc_time()) | second(utc_time()) |
+-------------+----------------+--------------------+--------------------+
| 2020 | 4 | 25 | 21 |
+-------------+----------------+--------------------+--------------------+
1 row in set (0.00 sec)
# 16. extract(type from date):从日期中按照type提取内容(type:YEAR、YEAR_MONTH、DAY_HOUR、DAY_MICROSECOND、DAY_MINUTE、DAY_SECOND)
mysql> select extract(year from now()),extract(year_month from now()),extract(day_hour from now());
+--------------------------+--------------------------------+------------------------------+
| extract(year from now()) | extract(year_month from now()) | extract(day_hour from now()) |
+--------------------------+--------------------------------+------------------------------+
| 2020 | 202011 | 11 |
+--------------------------+--------------------------------+------------------------------+
1 row in set (0.00 sec)
# 17. datediff(date1,date2):返回两个日期之间相差的天数
mysql> select datediff('2020-11-17','2019-11-17');
+-------------------------------------+
| datediff('2020-11-17','2019-11-17') |
+-------------------------------------+
| 366 |
+-------------------------------------+
1 row in set (0.00 sec)
# 18. time_to_sec(time):将时间转换成秒数
mysql> select time_to_sec(curtime());
+------------------------+
| time_to_sec(curtime()) |
+------------------------+
| 42101 |
+------------------------+
1 row in set (0.00 sec)
# 19. sec_to_time(second):将秒数转换成时间格式
mysql> select sec_to_time(42101);
+--------------------+
| sec_to_time(42101) |
+--------------------+
| 11:41:41 |
+--------------------+
1 row in set (0.00 sec)
五、系统信息函数
# 1. version():查看MySQL版本号
# 2. user()/current_user()/system_user()/session_user():查看当前被MySQL服务器验证的用户名和主机的组合,一般这几个函数的返回值是相同的
# 3. connection_id():查看当前用户的连接数
# 4. charset(str):查看字符串str使用的字符集
六、加密函数
# 1. password(str):
从原明文密码str计算并返回加密后的字符串密码,注意这个函数的加密是单向的(不可逆),因此不应将它应用在个人的应用程序中而应该只在MySQL服务器的鉴定系统中使用
MySQL5.7.9以后废弃
# 2. md5('str'):
为字符串算出一个MD5 128比特校验和,改值以32位十六进制数字的二进制字符串形式返回
# 3. encode(str, pswd_str):结果是一个二进制数,必须使用blob类型的字段来保存它;
# 4. decode(crypt_str,pswd_str):使用pswd_str作为密码,解密加密字符串crypt_str,crypt_str是由ENCODE函数返回的字符串
七、其他函数
# 1. format(x,n):格式化数字,并以四舍五入的方式保留小数点n位,结果是字符串
mysql> select format(2323.34,1);
+-------------------+
| format(2323.34,1) |
+-------------------+
| 2,323.3 |
+-------------------+
1 row in set (0.00 sec)
# 2. conv(N,from_base,to_base):不同进制数之间的转换,返回值为数值N的字符串表示,由from_base进制转换为
mysql> select conv(4,10,2);
+--------------+
| conv(4,10,2) |
+--------------+
| 100 |
+--------------+
1 row in set (0.00 sec)
# 3. hex(N):将十进制数字N转换成16进制;bin(N):将十进制数字N转换成二进制;oct(N):将十进制数字转换成8进制
mysql> select hex(10),bin(10),oct(10);
+---------+---------+---------+
| hex(10) | bin(10) | oct(10) |
+---------+---------+---------+
| A | 1010 | 12 |
+---------+---------+---------+
1 row in set (0.04 sec)
# 4. benchmark(count,expr):
重复执行count次表达式expr,它可以用于计算MySQL处理表达式的速度,结果值通常是0(0只是表示很快,并不是没有速度)。
另一个作用是用它在MySQL客户端内部报告语句执行的时间
mysql> select benchmark(3,9*3);
+------------------+
| benchmark(3,9*3) |
+------------------+
| 0 |
+------------------+
1 row in set (0.03 sec)
# 5. convert(str,type)/cast(str as type):可用来获取一个类型的值,并产生另一个类型的值。
type:值可以是
1)二进制,同带binary前缀的效果 : BINARY
2)字符型,可带参数 : CHAR()
3)日期 : DATE
4)时间: TIME
5)日期时间型 : DATETIME
6)浮点数 : DECIMAL
7)整数 : SIGNED
8)无符号整数 : UNSIGNED
mysql> select cast('235ad123' as signed),cast('235ad123' as unsigned),cast('235ad123' as BINARY);
+----------------------------+------------------------------+--------------------------------------------------------+
| cast('235ad123' as signed) | cast('235ad123' as unsigned) | cast('235ad123' as BINARY) |
+----------------------------+------------------------------+--------------------------------------------------------+
| 235 | 235 | 0x3233356164313233 |
+----------------------------+------------------------------+--------------------------------------------------------+
1 row in set, 2 warnings (0.03 sec)
mysql> select convert('235ad123',signed),convert('235ad123',unsigned),convert('235ad123',BINARY);
+----------------------------+------------------------------+--------------------------------------------------------+
| convert('235ad123',signed) | convert('235ad123',unsigned) | convert('235ad123',BINARY) |
+----------------------------+------------------------------+--------------------------------------------------------+
| 235 | 235 | 0x3233356164313233 |
+----------------------------+------------------------------+--------------------------------------------------------+
1 row in set, 2 warnings (0.00 sec)
八、聚合函数:
# 1. count(*):返回查询结果的总数
mysql> select count(*) from students;
+----------+
| count(*) |
+----------+
| 42 |
+----------+
1 row in set (0.33 sec)
# 2. sum(字段):返回查询字段的总和
mysql> select sum(score) from students;
+------------+
| sum(score) |
+------------+
| 2371 |
+------------+
1 row in set (0.00 sec)
# 3. avg(字段):返回查询字段的平均值
mysql> select avg(score) from students;
+------------+
| avg(score) |
+------------+
| 67.7429 |
+------------+
1 row in set (0.02 sec)
# 4. max(字段):返回查询字段的最大值;min(字段):返回查询字段的最小值
mysql> select max(score),min(score) from students;
+------------+------------+
| max(score) | min(score) |
+------------+------------+
| 100 | 0 |
+------------+------------+
1 row in set (0.02 sec)
更多MySQL函数请查阅:[https://www.jc2182.com/mysql/mysql-adddate.html](https://www.jc2182.com/mysql/mysql-adddate.html)