1.1 数值型函数
函数不能单独存在,需要与 select 等操作连用,否则会报错。 mysql> rand();
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'rand()' at line 1
函数名称
作 用
abs
求绝对值
sqrt
求二次方根
mod
求余数
ceil 和 ceiling
两个函数功能相同,都是返回不小于参数的最小整数,即向上取整
floor
向下取整,返回值转化为一个 bigint
rand
生成一个 0 ~ 1 之间的随机数,传入整数参数是,用来产生重复序列
round
对所传参数进行四舍五入
sign
返回参数的符号
pow 和 power
两个函数的功能相同,都是所传参数的次方的结果值
sin
求正弦值
asin
求反正弦值,与函数 sin 互为反函数
cos
求余弦值
acos
求反余弦值,与函数 cos 互为反函数
tan
求正切值
atan
求反正切值,与函数 tan 互为反函数
cot
求余切值
1.1.1 abs:求绝对值
mysql> select abs( 1) , abs( 0) , abs( -1) ;
+--------+--------+---------+
| abs( 1) | abs( 0) | abs( -1) |
+--------+--------+---------+
| 1 | 0 | 1 |
+--------+--------+---------+
1 row in set ( 0.08 sec)
1.1.2 sqrt:开方
mysql> select sqrt( -9) , sqrt( 4) , sqrt( 2) , sqrt( 0) ;
+----------+---------+--------------------+---------+
| sqrt( -9) | sqrt( 4) | sqrt( 2) | sqrt( 0) |
+----------+---------+--------------------+---------+
| NULL | 2 | 1.4142135623730951 | 0 |
+----------+---------+--------------------+---------+
1 row in set ( 0.07 sec)
1.1.3 mod:取余
mysql> select mod( 8,2) , mod( 9,2) , mod( 15.3,2) ;
+----------+----------+-------------+
| mod( 8,2) | mod( 9,2) | mod( 15.3,2) |
+----------+----------+-------------+
| 0 | 1 | 1.3 |
+----------+----------+-------------+
1 row in set ( 0.39 sec)
1.1.4 ceil:向上取整
mysql> select ceil( -3.3) , ceil( 2.2) , ceil( 0) ;
+------------+-----------+---------+
| ceil( -3.3) | ceil( 2.2) | ceil( 0) |
+------------+-----------+---------+
| -3 | 3 | 0 |
+------------+-----------+---------+
1 row in set ( 19.50 sec)
1.1.5 floor:向下取整
mysql> select floor( -3.3) , floor( 2.2) , floor( 0) ;
+-------------+------------+----------+
| floor( -3.3) | floor( 2.2) | floor( 0) |
+-------------+------------+----------+
| -4 | 2 | 0 |
+-------------+------------+----------+
1 row in set ( 0.09 sec)
1.1.6 rand:生成随机数
mysql> select rand( ) ,rand( ) ,rand( ) ;
+--------------------+--------------------+----------------------+
| rand( ) | rand( ) | rand( ) |
+--------------------+--------------------+----------------------+
| 0.2219784559886702 | 0.9371273414577612 | 0.019701370056440466 |
+--------------------+--------------------+----------------------+
1 row in set ( 0.11 sec)
mysql> select rand( 1) ,rand( 1) ,rand( 2) ,rand( 2) ;
+---------------------+---------------------+--------------------+--------------------+
| rand( 1) | rand( 1) | rand( 2) | rand( 2) |
+---------------------+---------------------+--------------------+--------------------+
| 0.40540353712197724 | 0.40540353712197724 | 0.6555866465490187 | 0.6555866465490187 |
+---------------------+---------------------+--------------------+--------------------+
1 row in set ( 0.09 sec)
1.1.7 round:四舍五入
mysql> select round( 3.4) ,round( -3.4) ,round( 3.5) ,round( -3.5) ,round( 3.6) ,round( -3.6) ;
+------------+-------------+------------+-------------+------------+-------------+
| round( 3.4) | round( -3.4) | round( 3.5) | round( -3.5) | round( 3.6) | round( -3.6) |
+------------+-------------+------------+-------------+------------+-------------+
| 3 | -3 | 4 | -4 | 4 | -4 |
+------------+-------------+------------+-------------+------------+-------------+
1 row in set ( 0.10 sec)
mysql> select round( 3.3,2) ,round( -3.3,3) ,round( 8,-1) ,round( 88.33,-2) ;
+--------------+---------------+-------------+-----------------+
| round( 3.3,2) | round( -3.3,3) | round( 8,-1) | round( 88.33,-2) |
+--------------+---------------+-------------+-----------------+
| 3.30 | -3.300 | 10 | 100 |
+--------------+---------------+-------------+-----------------+
1 row in set ( 0.09 sec)
1.1.8 sign:返回符号
mysql> select sign( -5) ,sign( 5) ,sign( 0) ;
+----------+---------+---------+
| sign( -5) | sign( 5) | sign( 0) |
+----------+---------+---------+
| -1 | 1 | 0 |
+----------+---------+---------+
1 row in set ( 0.08 sec)
1.1.9 pow:次方根
mysql> select pow( 3,3) ,pow( 3.3,2) ,pow( -2,3) ,pow( 2.1,2) ;
+----------+--------------------+-----------+------------+
| pow( 3,3) | pow( 3.3,2) | pow( -2,3) | pow( 2.1,2) |
+----------+--------------------+-----------+------------+
| 27 | 10.889999999999999 | -8 | 4.41 |
+----------+--------------------+-----------+------------+
1 row in set ( 0.09 sec)
1.2 字符串函数
函数名称
作 用
length
计算字符串长度函数,返回字符串的字节长度
concat
合并字符串函数,返回结果为连接参数产生的字符串,参数可以使一个或多个
insert
替换字符串函数
lower
将字符串中的字母转换为小写
upper
将字符串中的字母转换为大写
left
从左侧字截取符串,返回字符串左边的若干个字符
right
从右侧字截取符串,返回字符串右边的若干个字符
trim
删除字符串左右两侧的空格
replace
字符串替换函数,返回替换后的新字符串
substring
截取字符串,返回从指定位置开始的指定长度的字符换
reverse
字符串反转(逆序)函数,返回与原始字符串顺序相反的字符串
1.2.1 length:长度
mysql> select length( 12315) ,length( 'abcd' ) ,length( '德玛西亚' ) ,length( '死亡如风·DDDD' ) ;
+---------------+----------------+--------------------+-------------------------+
| length( 12315) | length( 'abcd' ) | length( '德玛西亚' ) | length( '死亡如风·DDDD' ) |
+---------------+----------------+--------------------+-------------------------+
| 5 | 4 | 12 | 18 |
+---------------+----------------+--------------------+-------------------------+
1 row in set ( 1.52 sec)
1.2.2 concat:拼接
mysql> select concat( 123,'def' ) ,concat( '亚洲' ,'中国' ) ,concat( 'aaa' ,null) ;
+-------------------+-----------------------+--------------------+
| concat( 123,'def' ) | concat( '亚洲' ,'中国' ) | concat( 'aaa' ,null) |
+-------------------+-----------------------+--------------------+
| 123def | 亚洲中国 | NULL |
+-------------------+-----------------------+--------------------+
1 row in set ( 0.11 sec)
1.2.3 left:从左侧截取字符串
mysql> select left( 'abcdefg' ,3) ;
+-------------------+
| left( 'abcdefg' ,3) |
+-------------------+
| abc |
+-------------------+
1 row in set ( 0.14 sec)
1.2.4 trim:去除空格
mysql> select length( ' aaa ' ) ,length( trim( ' aaa ' )) ,length( ' 天天 ' ) ,length( trim( ' 天天 ' )) ;
+-----------------+-----------------------+-------------------+-------------------------+
| length( ' aaa ' ) | length( trim( ' aaa ' )) | length( ' 天天 ' ) | length( trim( ' 天天 ' )) |
+-----------------+-----------------------+-------------------+-------------------------+
| 5 | 3 | 9 | 6 |
+-----------------+-----------------------+-------------------+-------------------------+
1 row in set ( 0.18 sec)
1.3 事件日期函数
函数名称
作 用
curdate 和 current_date
两个函数作用相同,返回当前系统的日期值
curtime 和 current_time
两个函数作用相同,返回当前系统的时间值
now 和 sysdate
两个函数作用相同,返回当前系统的日期和时间值
unix_timestamp
获取 unix 时间戳函数,返回一个以 unix 时间戳为基础的无符号整数
from_unixtime
将 unix 时间戳转换为时间格式,与 unix_timestamp 互为反函数
month
获取指定日期中的月份
monthname
获取指定日期中的月份英文名称
dayname
获取指定曰期对应的星期几的英文名称
dayofweek
获取指定日期对应的一周的索引位置值
week
获取指定日期是一年中的第几周,返回值的范围是否为 0 〜 52 或 1 〜 53
dayofyear
获取指定曰期是一年中的第几天,返回值范围是 1 ~ 366
dayofmonth
获取指定日期是一个月中是第几天,返回值范围是 1 ~ 31
year
获取年份,返回值范围是 1970 〜 2069
time_to_sec
将时间参数转换为秒数
sec_to_time
将秒数转换为时间,与 time_to_sec 互为反函数
date_add 和 adddate
两个函数功能相同,都是向日期添加指定的时间间隔
date_sub 和 subdate
两个函数功能相同,都是向日期减去指定的时间间隔
addtime
时间加法运算,在原始时间上添加指定的时间
subtime
时间减法运算,在原始时间上减去指定的时间
datediff
获取两个日期之间间隔,返回参数 1 减去参数 2 的值
date_format
格式化指定的日期,根据参数返回指定格式的值
weekday
获取指定日期在一周内的对应的工作日索引
1.3.1 curdate:系统日期
mysql> select curdate( ) ,current_date( ) ,current_date( ) + 1;
+------------+----------------+--------------------+
| curdate( ) | current_date( ) | current_date( ) + 1 |
+------------+----------------+--------------------+
| 2020-07-06 | 2020-07-06 | 20200707 |
+------------+----------------+--------------------+
1 row in set ( 0.12 sec)
1.3.2 curtime:系统时间
mysql> select curtime( ) ,current_time( ) ,current_time( ) + 1;
+-----------+----------------+--------------------+
| curtime( ) | current_time( ) | current_time( ) + 1 |
+-----------+----------------+--------------------+
| 11:16:11 | 11:16:11 | 111612 |
+-----------+----------------+--------------------+
1 row in set ( 0.11 sec)
1.3.3 sysdate:系统时间日期
mysql> select now( ) ,now( ) + 1,sysdate( ) ,sysdate( ) + 1;
+---------------------+----------------+---------------------+----------------+
| now( ) | now( ) + 1 | sysdate( ) | sysdate( ) + 1 |
+---------------------+----------------+---------------------+----------------+
| 2020-07-06 11:18:58 | 20200706111859 | 2020-07-06 11:18:58 | 20200706111859 |
+---------------------+----------------+---------------------+----------------+
1 row in set ( 0.09 sec)
1.3.4 unix_timestamp:时间戳
mysql> select unix_timestamp( ) ,unix_timestamp( now( )) ,unix_timestamp( '2020-01-01 12:12:12' ) ;
+------------------+-----------------------+---------------------------------------+
| unix_timestamp( ) | unix_timestamp( now( )) | unix_timestamp( '2020-01-01 12:12:12' ) |
+------------------+-----------------------+---------------------------------------+
| 1594005672 | 1594005672 | 1577851932 |
+------------------+-----------------------+---------------------------------------+
1 row in set ( 0.09 sec)
mysql> select from_unixtime( 1594005656) ,from_unixtime( 1594005656,'%Y 年 %m 月 %d 日 %H:%h:%s' ) ;
+---------------------------+--------------------------------------------------------+
| from_unixtime( 1594005656) | from_unixtime( 1594005656,'%Y 年 %m 月 %d 日 %H:%h:%s' ) |
+---------------------------+--------------------------------------------------------+
| 2020-07-06 11:20:56 | 2020 年 07 月 06 日 11:11:56 |
+---------------------------+--------------------------------------------------------+
1 row in set ( 0.10 sec)
1.3.5 month:获取指定日期的月份
mysql> select month( '2020-01-30' ) ,month( now( )) ;
+---------------------+--------------+
| month( '2020-01-30' ) | month( now( )) |
+---------------------+--------------+
| 1 | 7 |
+---------------------+--------------+
1 row in set ( 0.11 sec)
mysql> select monthname( '2020-01-30' ) ,monthname( now( )) ;
+-------------------------+------------------+
| monthname( '2020-01-30' ) | monthname( now( )) |
+-------------------------+------------------+
| January | July |
+-------------------------+------------------+
1 row in set ( 0.12 sec)
mysql> select now( ) ,dayname( now( )) ;
+---------------------+----------------+
| now( ) | dayname( now( )) |
+---------------------+----------------+
| 2020-07-06 11:27:27 | Monday |
+---------------------+----------------+
1 row in set ( 0.12 sec)
mysql> select now( ) ,year( now( )) ,year( '1997-01-01' ) ;
+---------------------+-------------+--------------------+
| now( ) | year( now( )) | year( '1997-01-01' ) |
+---------------------+-------------+--------------------+
| 2020-07-06 11:28:48 | 2020 | 1997 |
+---------------------+-------------+--------------------+
1 row in set ( 0.10 sec)
1.3.6 时间日期计算
mysql> select date_add( '2020-01-01' ,interval 100 second) ,adddate( now( ) ,interval 10 day) ;
+--------------------------------------------+--------------------------------+
| date_add( '2020-01-01' ,interval 100 second) | adddate( now( ) ,interval 10 day) |
+--------------------------------------------+--------------------------------+
| 2020-01-01 00:01:40 | 2020-07-16 11:34:01 |
+--------------------------------------------+--------------------------------+
1 row in set ( 0.11 sec)
mysql> select date_sub( '2020-01-01' ,interval 100 second) ,subdate( now( ) ,interval 10 day) ;
+--------------------------------------------+--------------------------------+
| date_sub( '2020-01-01' ,interval 100 second) | subdate( now( ) ,interval 10 day) |
+--------------------------------------------+--------------------------------+
| 2019-12-31 23:58:20 | 2020-06-26 11:37:39 |
+--------------------------------------------+--------------------------------+
1 row in set ( 0.09 sec)
mysql> select addtime( '2020-01-01 23:59:59' ,'00:00:01' ) , addtime( curtime( ) ,'01:00:00' ) ;
+-------------------------------------------+-------------------------------+
| addtime( '2020-01-01 23:59:59' ,'00:00:01' ) | addtime( curtime( ) ,'01:00:00' ) |
+-------------------------------------------+-------------------------------+
| 2020-01-02 00:00:00 | 12:39:04 |
+-------------------------------------------+-------------------------------+
1 row in set ( 0.10 sec)
mysql> select subtime( '2020-01-01 00:00:00' ,'00:00:01' ) , subtime( curtime( ) ,'01:00:00' ) ;
+-------------------------------------------+-------------------------------+
| subtime( '2020-01-01 00:00:00' ,'00:00:01' ) | subtime( curtime( ) ,'01:00:00' ) |
+-------------------------------------------+-------------------------------+
| 2019-12-31 23:59:59 | 10:40:47 |
+-------------------------------------------+-------------------------------+
1 row in set ( 0.08 sec)
mysql> select datediff( '1997-01-01' ,curdate( )) ;
+----------------------------------+
| datediff( '1997-01-01' ,curdate( )) |
+----------------------------------+
| -8587 |
+----------------------------------+
1 row in set ( 0.10 sec)
1.4 其他函数
函数名称
作 用
version
数据库版本号
database
当前的数据库
user
当前连接用户
password
返回字符串密码形式,即密码密文
md5
返回字符串的 md5 数据
1.4.1 version:版本号
mysql> select version( ) ;
+------------+
| version( ) |
+------------+
| 5.7.19-log |
+------------+
1 row in set ( 0.09 sec)
1.4.2 user:当前连接用户
mysql> select user( ) ;
+--------------------+
| user( ) |
+--------------------+
| [email protected] |
+--------------------+
1 row in set ( 0.07 sec)
1.4.3 password:密码密文
mysql> select password( '123456' ) ;
+-------------------------------------------+
| password( '123456' ) |
+-------------------------------------------+
| *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+-------------------------------------------+
1 row in set ( 0.08 sec)
1.4.4 MD5:加密
mysql> select md5( '123456' ) ;
+----------------------------------+
| md5( '123456' ) |
+----------------------------------+
| e10adc3949ba59abbe56e057f20f883e |
+----------------------------------+
1 row in set ( 0.08 sec)
关注公众号一起学习 Java 开发,跟大家分享我的学习成长过程