返回绝对值。
mysql> select abs(-4);
+---------+
| abs(-4) |
+---------+
| 4 |
+---------+
1 row in set (0.00 sec)
返回圆周率,并四舍五入保留五位小数。
mysql> select pi();
+----------+
| pi() |
+----------+
| 3.141593 |
+----------+
1 row in set (0.00 sec)
返回一个0-1的随机浮点数,包含0和1,如果指定值则它被用作种子值,用来产生重复序列。
mysql> select rand(),rand(2),rand(2);
+-------------------+--------------------+--------------------+
| rand() | rand(2) | rand(2) |
+-------------------+--------------------+--------------------+
| 0.999596054665368 | 0.6555866465490187 | 0.6555866465490187 |
+-------------------+--------------------+--------------------+
1 row in set (0.00 sec)
平方根函数,返回一个非负数的平方根,如果x为负数则返回NULL值。
mysql> select sqrt(2),sqrt(9),sqrt(-25);
+--------------------+---------+-----------+
| sqrt(2) | sqrt(9) | sqrt(-25) |
+--------------------+---------+-----------+
| 1.4142135623730951 | 3 | NULL |
+--------------------+---------+-----------+
1 row in set (0.00 sec)
求余数函数,也叫取模函数
mysql> select mod(7,2),mod(9,3);
+----------+----------+
| mod(7,2) | mod(9,3) |
+----------+----------+
| 1 | 0 |
+----------+----------+
1 row in set (0.00 sec)
取整函数,向上取整,取X整数部分再加1。
mysql> select ceil(3.78),ceil(7.43),ceiling(4.78),ceiling(2.23);
+------------+------------+---------------+---------------+
| ceil(3.78) | ceil(7.43) | ceiling(4.78) | ceiling(2.23) |
+------------+------------+---------------+---------------+
| 4 | 8 | 5 | 3 |
+------------+------------+---------------+---------------+
1 row in set (0.00 sec)
取整函数,向下取整,只取X整数部分。
mysql> select floor(6.98),floor(3.25);
+-------------+-------------+
| floor(6.98) | floor(3.25) |
+-------------+-------------+
| 6 | 3 |
+-------------+-------------+
1 row in set (0.00 sec)
ROUND(X)取整函数,取X接近的整数,对X的值进行四舍五入;ROUND(X,Y)返回X参数,指定Y个小数位,也是对小数部分Y后面的位数进行四舍五入。
mysql> select round(8.76),round(56.765,2);
+-------------+-----------------+
| round(8.76) | round(56.765,2) |
+-------------+-----------------+
| 9 | 56.77 |
+-------------+-----------------+
1 row in set (0.00 sec)
POW(X,Y)、POWER(X,Y)返回的结果一样,X的Y次方,EXP(X)的是e的X次方,e的近似值为2.71828 。
mysql> select pow(2,3),power(2,10),exp(2);
+----------+-------------+------------------+
| pow(2,3) | power(2,10) | exp(2) |
+----------+-------------+------------------+
| 8 | 1024 | 7.38905609893065 |
+----------+-------------+------------------+
1 row in set (0.01 sec)
LOG(X)返回x的自然对数e,LOGN(X)是以N为底数的X的对数。
mysql> select log(2),log2(2),log10(100);
+--------------------+---------+------------+
| log(2) | log2(2) | log10(100) |
+--------------------+---------+------------+
| 0.6931471805599453 | 1 | 2 |
+--------------------+---------+------------+
1 row in set (0.00 sec)
CHAR_LENGTH(STR)返回值为字符串STR所有包含的字符个数,一个多字符算作一个单字符,LENGTH(STR)返回值为字符串的字节长度,使用UTF8的一种可变长字符编码,一个汉字是三个字节,一个数字或者一个字母算一个字节。
mysql> select char_length('aabb'),char_length('lllsss'),length('哈哈哈'),length('shsj211');
+---------------------+-----------------------+---------------------+-------------------+
| char_length('aabb') | char_length('lllsss') | length('哈哈哈') | length('shsj211') |
+---------------------+-----------------------+---------------------+-------------------+
| 4 | 6 | 9 | 7 |
+---------------------+-----------------------+---------------------+-------------------+
1 row in set (0.00 sec)
返回的值结果为连接参数产生的字符串,可一个或多个参数,如过其中一个参数为NULL则返回值就为NULL。
mysql> select concat('hhsh','988',' ll'),concat('hdjh',null,'ooio');
+------------------------------+----------------------------+
| concat('hhsh','988',' ll') | concat('hdjh',null,'ooio') |
+------------------------------+----------------------------+
| hhsh988 ll | NULL |
+------------------------------+----------------------------+
1 row in set (0.00 sec)
x参数是分隔符,分隔符在两个连续的字符串之间。分隔符可以是一个字串,也可以是其他字符,如果分隔符为NULL则结果返回为NULL,函数会忽略任何分隔符参数后的NULL值,其他参数为NULL值,不会影响拼接。
mysql> select concat_ws('=>','hdh','kkk','lll','yyy'),concat_ws('-','iii',null,'ppp'),concat_ws(null,'kkk','ppp','uuu');
+-----------------------------------------+---------------------------------+-----------------------------------+
| concat_ws('=>','hdh','kkk','lll','yyy') | concat_ws('-','iii',null,'ppp') | concat_ws(null,'kkk','ppp','uuu') |
+-----------------------------------------+---------------------------------+-----------------------------------+
| hdh=>kkk=>lll=>yyy | iii-ppp | NULL |
+-----------------------------------------+---------------------------------+-----------------------------------+
1 row in set (0.00 sec)
替换字符串函数,返回字符串s1,x为替换替换字符的起始位置,len为替换的长度,s2为替换的字符或字符串。
mysql> select insert('aaahhhss',4,3,'iii');
+------------------------------+
| insert('aaahhhss',4,3,'iii') |
+------------------------------+
| aaaiiiss |
+------------------------------+
1 row in set (0.00 sec)
LOWER(STR)或LCASE(STR)可以将字符串STR字符替换为小写字母。UPPER(STR)可以将字符串STR字符串字符替换为大写字母。
mysql> select lower('AAA'),LCASE('BBHHH'),UPPER('kkkk');
+--------------+----------------+---------------+
| lower('AAA') | LCASE('BBHHH') | UPPER('kkkk') |
+--------------+----------------+---------------+
| aaa | bbhhh | KKKK |
+--------------+----------------+---------------+
1 row in set (0.00 sec)
LEFT(STR,N)返回字符串STR开始的最左边N个字符串,RIGHT(STR,N)返回字符串STR最右边N个字符串。
mysql> select left('hhshkkss',4),right('uuuaaa',3);
+--------------------+-------------------+
| left('hhshkkss',4) | right('uuuaaa',3) |
+--------------------+-------------------+
| hhsh | aaa |
+--------------------+-------------------+
1 row in set (0.00 sec)
LTRIM(STR)返回去掉左边空格的字符串、RTRIM(STR)返回去掉右边空格的字符串、TRIM(STR)去掉两边空格后的字符串。
mysql> select ltrim(' jjhjhj '),rtrim(' djfijd '),trim(' jhjdhfj ');
+----------------------+-------------------------+-----------------------+
| ltrim(' jjhjhj ') | rtrim(' djfijd ') | trim(' jhjdhfj ') |
+----------------------+-------------------------+-----------------------+
| jjhjhj | djfijd | jhjdhfj |
+----------------------+-------------------------+-----------------------+
1 row in set (0.00 sec)
产生N个空格字符。
mysql> select concat('[',space(5),']');
+--------------------------+
| concat('[',space(5),']') |
+--------------------------+
| [ ] |
+--------------------------+
1 row in set (0.00 sec)
返回使用字符串STR2替代字符串S中所有的字符串STR1。
mysql> select replace('aabb','a','c');
+-------------------------+
| replace('aabb','a','c') |
+-------------------------+
| ccbb |
+-------------------------+
1 row in set (0.00 sec)
比较字符串函数,若STR1等于STR2就返回0,若STR1大于STR2就返回1,若字符串STR1小于STR2就返回-1。
mysql> select strcmp('aa','aa'),strcmp('aa1','aa'),strcmp('aa','aa1');
+-------------------+--------------------+--------------------+
| strcmp('aa','aa') | strcmp('aa1','aa') | strcmp('aa','aa1') |
+-------------------+--------------------+--------------------+
| 0 | 1 | -1 |
+-------------------+--------------------+--------------------+
1 row in set (0.00 sec)
逆置字符串函数。
mysql> select reverse('hhhsssbbb');
+----------------------+
| reverse('hhhsssbbb') |
+----------------------+
| bbbssshhh |
+----------------------+
1 row in set (0.00 sec)
返回STR1在字符串STR中的起始位置
mysql> select locate('ss','aaaddsskkll'),position('bb' in 'shdhjavbbjkj');
+----------------------------+----------------------------------+
| locate('ss','aaaddsskkll') | position('bb' in 'shdhjavbbjkj') |
+----------------------------+----------------------------------+
| 6 | 8 |
+----------------------------+----------------------------------+
1 row in set (0.00 sec)
获取当前日期,格式为YYYY-MM-DD格式
mysql> select curdate(),current_date();
+------------+----------------+
| curdate() | current_date() |
+------------+----------------+
| 2023-08-19 | 2023-08-19 |
+------------+----------------+
1 row in set (0.00 sec)
获取当前日期和时间
mysql> select current_timestamp(),localtime(),now(),sysdate();
+---------------------+---------------------+---------------------+---------------------+
| current_timestamp() | localtime() | now() | sysdate() |
+---------------------+---------------------+---------------------+---------------------+
| 2023-08-19 19:17:53 | 2023-08-19 19:17:53 | 2023-08-19 19:17:53 | 2023-08-19 19:17:53 |
+---------------------+---------------------+---------------------+---------------------+
1 row in set (0.00 sec)
获取日期月份
mysql> select year(now());
+-------------+
| year(now()) |
+-------------+
| 2023 |
+-------------+
1 row in set (0.00 sec)
获取日期月份,MONTHNAME(DATE)格式为英文。
mysql> select month(now()),monthname(now());
+--------------+------------------+
| month(now()) | monthname(now()) |
+--------------+------------------+
| 8 | August |
+--------------+------------------+
1 row in set (0.00 sec)
获取具体的日
mysql> select day(now());
+------------+
| day(now()) |
+------------+
| 19 |
+------------+
1 row in set (0.00 sec)
获取星期几
mysql> select dayname(now()),dayofweek(now()),weekday(now());
+----------------+------------------+----------------+
| dayname(now()) | dayofweek(now()) | weekday(now()) |
+----------------+------------------+----------------+
| Saturday | 7 | 5 |
+----------------+------------------+----------------+
1 row in set (0.00 sec)
DAYOFYEAR(DATE)返回值为这一年的第几天。
DAYOFMONTH(DATE)返回值为这一年的第几月。
mysql> select dayofmonth(now()),dayofyear(now());
+-------------------+------------------+
| dayofmonth(now()) | dayofyear(now()) |
+-------------------+------------------+
| 19 | 231 |
+-------------------+------------------+
1 row in set (0.00 sec)
将时间转换为秒。
mysql> select time_to_sec('14:20:30'),time_to_sec(time(now()));
+-------------------------+--------------------------+
| time_to_sec('14:20:30') | time_to_sec(time(now())) |
+-------------------------+--------------------------+
| 51630 | 70443 |
+-------------------------+--------------------------+
1 row in set (0.00 sec)
如果表达式EXPR为TRUE就返回V1,否则返回V2,IF()函数返回的值可以是数字值或者字符串值。
mysql> select if(2>6,'YES','NO');
+--------------------+
| if(2>6,'YES','NO') |
+--------------------+
| NO |
+--------------------+
1 row in set (0.00 sec)
返回值如果V1不会NULL就返回V1,否则返回V2。
mysql> select ifnull(null,'aa'),ifnull(123,777);
+-------------------+-----------------+
| ifnull(null,'aa') | ifnull(123,777) |
+-------------------+-----------------+
| aa | 123 |
+-------------------+-----------------+
1 row in set (0.00 sec)
返回值为当EXPR的值等于Vn就返回对应Rn的值。当都不满足Vn的值有ELSE语句就返回后的值。
mysql> select case 3
-> when 1 then 'a'
-> when 2 then 'b'
-> when 3 then 'c'
-> when 4 then 'd'
-> else 'e' end;
+-------------------------------------------------------------------------------------+
| case 3
when 1 then 'a'
when 2 then 'b'
when 3 then 'c'
when 4 then 'd'
else 'e' end |
+-------------------------------------------------------------------------------------+
| c |
+-------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
返回MySQL的版本号、连接数、数据库名的函数。
mysql> select version(),connection_id(),database();
+-----------+-----------------+------------+
| version() | connection_id() | database() |
+-----------+-----------------+------------+
| 5.7.18 | 3 | db1 |
+-----------+-----------------+------------+
1 row in set (0.00 sec)
返回当前登录用户名,连接会话的用户。
mysql> select USER(),SESSION_USER();
+----------------+----------------+
| USER() | SESSION_USER() |
+----------------+----------------+
| root@localhost | root@localhost |
+----------------+----------------+
1 row in set (0.00 sec)
返回一个加密后的字符串,此函数是单向加密不可逆。
mysql> select password('123aaabbb');
+-------------------------------------------+
| password('123aaabbb') |
+-------------------------------------------+
| *616E8E68832C222FFAEB396A9FA75865A6F7F039 |
+-------------------------------------------+
1 row in set, 1 warning (0.00 sec)
返回一个MD5 128比特校验和,该值以32位十六进制数字的二进制字符串形式返回。若参数为NULL则返回NULL。
mysql> select md5('hhhss12234'),md5(null);
+----------------------------------+-----------+
| md5('hhhss12234') | md5(null) |
+----------------------------------+-----------+
| 2c6efa51fe416f1c23d716bab5be9e4e | NULL |
+----------------------------------+-----------+
1 row in set (0.00 sec)
使用PSWD作为密码,加密STR,使用DECODE()解密结果,是一个和STR长度相同的二进制字符串。
mysql> select encode('123hhh','aaa123');
+---------------------------+
| encode('123hhh','aaa123') |
+---------------------------+
| sbx' |
+---------------------------+
1 row in set, 1 warning (0.00 sec)
使用PASSD_STR作为密码,解密加密字符串CRYPT_STR,CRYPT_STR是由ENCODE()返回的字符串。
mysql> select decode(encode('123hhh','aaa123'),'aaa123');
+--------------------------------------------+
| decode(encode('123hhh','aaa123'),'aaa123') |
+--------------------------------------------+
| 123hhh |
+--------------------------------------------+
1 row in set, 2 warnings (0.00 sec)
参考书籍:MySQL5.7从入门到精通-清华大学出版社