Welcome 微信登录

首页 / 数据库 / MySQL / MySQL常用函数汇总

一、数学函数1.绝对值函数abs(value) mysql> select abs(-120);
+-----------+
| abs(-120) |
+-----------+
|     120 |
+-----------+
1 row in set (0.00 sec)2.地板函数floor(value),获取小于value的最大整数mysql> select floor(23.8);
+-------------+
| floor(23.8) |
+-------------+
|          23 |
+-------------+
1 row in set (0.00 sec)3.天花板函数ceiling(value),获取大于value的最小整数mysql> select ceiling(23.8);
+---------------+
| ceiling(23.8) |
+---------------+
|            24 |
+---------------+
1 row in set (0.00 sec)4.四舍五入函数round(value,position),四舍五入到小数点后几位mysql> select round(23.27); --默认是0
+--------------+
| round(23.27) |
+--------------+
|         23 |
+--------------+
1 row in set (0.00 sec)mysql> select round(23.27,1);
+----------------+
| round(23.27,1) |
+----------------+
|         23.3 |
+----------------+
1 row in set (0.00 sec)5.随机数函数rand(),获取0到1之间随机数mysql> select rand();
+---------------------+
| rand()              |
+---------------------+
| 0.12718137365503365 |
+---------------------+
1 row in set (0.00 sec)二、字符串函数1.连接函数concat(str1,str2)mysql> select concat("hello","world");
+-------------------------+
| concat("hello","world") |
+-------------------------+
| helloworld              |
+-------------------------+
1 row in set (0.00 sec)2.带分隔符的连接函数concat_ws(separator,str1,str2)mysql> select concat_ws("#","hello","world");
+--------------------------------+
| concat_ws("#","hello","world") |
+--------------------------------+
| hello#world                    |
+--------------------------------+
1 row in set (0.00 sec)3.字符串所占字节数length(str)mysql> select length("helloworld我");
+------------------------+
| length("helloworld我") |
+------------------------+
|                   12 |
+------------------------+
1 row in set (0.00 sec)4.字符串的字符个数char_length(str)mysql> select char_length("helloworld我");
+-----------------------------+
| char_length("helloworld我") |
+-----------------------------+
|                          12 |
+-----------------------------+
1 row in set (0.00 sec)5.字符串判断函数a. IF(exp1,exp2,exp3):若是exp1 为真,返回exp2;若是exp1为假,返回exp3mysql> select if(2>1,"hello","world");
+-------------------------+
| if(2>1,"hello","world") |
+-------------------------+
| hello                 |
+-------------------------+
1 row in set (0.00 sec)mysql> select if(null=1,"hello","world");
+----------------------------+
| if(null=1,"hello","world") |
+----------------------------+
| world                      |
+----------------------------+
1 row in set (0.08 sec)b. IFNULL(exp1,exp2):若是exp1 IS NOT NULL,返回exp1,否则返回exp2 mysql> select IFNULL("hello","world");
+-------------------------+
| IFNULL("hello","world") |
+-------------------------+
| hello                 |
+-------------------------+
1 row in set (0.00 sec)mysql> select IFNULL(null,"world");
+----------------------+
| IFNULL(null,"world") |
+----------------------+
| world                |
+----------------------+
1 row in set (0.06 sec)c. NULLIF(exp1,exp2):若是exp1=exp2,返回NULL,否则返回exp1mysql> select NULLIF("hello","hello");
+-------------------------+
| NULLIF("hello","hello") |
+-------------------------+
| NULL                    |
+-------------------------+
1 row in set (0.00 sec)mysql> select NULLIF("hello","hello11");
+---------------------------+
| NULLIF("hello","hello11") |
+---------------------------+
| hello                   |
+---------------------------+
1 row in set (0.00 sec)6. 字符串转换函数a. LTRIM(exp1):去掉exp1中字符串开头(LEFT)的空格mysql> select LTRIM("  HELLO");
+------------------+
| LTRIM("  HELLO") |
+------------------+
| HELLO            |
+------------------+
1 row in set (0.00 sec)b.RTRIM(exp1):去掉exp1中字符串结尾(RIGHT)的空格mysql> select LTRIM(" HELLO     ");
+--------------------------+
| LTRIM(" HELLO     ") |
+--------------------------+
| HELLO                    |
+--------------------------+
1 row in set (0.00 sec)c.TRIM(exp1):去掉exp1中的开头和 结尾的空格mysql> select TRIM(" HELLO     ");
+-------------------------+
| TRIM(" HELLO     ") |
+-------------------------+
| HELLO                 |
+-------------------------+
1 row in set (0.00 sec)7.字符串查找函数a.SUBSTRING_INDEX(exp1,delim,count),其中exp1为字符串,delim为分割符号,count表示第几个风格符号mysql> select substring_index("1121.qq..com",".",1);
+---------------------------------------+
| substring_index("1121.qq..com",".",1) |
+---------------------------------------+
| 1121                                  |
+---------------------------------------+
1 row in set (0.00 sec)b.  SUBSTRING(exp1,pos,len): exp1为字符串,pos为位置,len为长度mysql> select substring("1121.qq.com",1,9);
+------------------------------+
| substring("1121.qq.com",1,9) |
+------------------------------+
| 1121.qq.c                    |
+----------------------------+
1 row in set (0.00 sec)8.日期操作函数
日期格式转换函数
将字符串转换成日期方式,DATE_FORMAT() 或STR_TO_DATE(),两个函数的格式如下
DATE_FORMAT(expr1,format)
STR_TO_DATE(expr1, format)a
常用的日期格式YYYY-MM-DD HH:MM:SS 对应的format为%Y-%m-%d %H:%i:%S
日期运算函数
CURDATE():返回当天的日期,格式: YYYY-MM-DD
NOW():返回当下的日期时间,格式: YYYY-MM-DD HH:MM:SS
DATE_ADD(date,INTERVAL expr unit):expr 表达式,unit为单位,例如:
DATE_ADD(‘2013-08-20’,INTERVAL -1 DAY),返回:2013-08-199.日期运算函数
MONTH(date):返回date的月份数,例如MONTY(‘2013-08-20’),返回:8
DAY(date):返回date的天数,例如DAY(‘2013-08-20’),返回:20
YEAR(date):返回date的年份数,例如YEAR(‘2013-08-20’),返回:2013
TO_DAYS(date):date对应的天数,例如TO_DAYS (‘2008-10-07’),返回:733687
WEEK(date):返回date对应周几,例如: WEEK(‘2013-08-20’),返回:2
DATEDIFF(date1,date2):返回date1-date2的天数差别,例如:
DATEDIFF(‘2013-08-20’,’2013-08-18 13:30:14’),返回:210.类型转换函数
CAST(expr  AS  type):expr转换成某类型
CONVERT(expr , type) : expr转换成某类型
CONVERT(expr  USING  transcoding_name):expr字符集转换成某字符集本文永久更新链接地址