前言:
mysql数据库中提供了很丰富的函数,比如我们常用的聚合函数,日期及字符串处理函数等。select语句及其条件表达式都可以使用这些函数,函数可以帮助用户更加方便的处理表中的数据,使mysql数据库的功能更加强大。本篇文章主要为大家介绍几类常用函数的用法。
聚合函数是平时比较常用的一类函数,这里列举如下:
数值型函数主要是对数值型数据进行处理,得到我们想要的结果,常用的几个列举如下,具体使用方法大家可以试试看。
一些示例:
# abs()函数求绝对值 mysql> select abs(5),abs(-2.4),abs(-24),abs(0); +--------+-----------+----------+--------+ | abs(5) | abs(-2.4) | abs(-24) | abs(0) | +--------+-----------+----------+--------+ | 5 | 2.4 | 24 | 0 | +--------+-----------+----------+--------+ # 取整函数 ceil(x) 和 ceiling(x) 的意义相同,返回不小于 x 的最小整数值 mysql> select ceil(-2.5),ceiling(2.5); +------------+--------------+ | ceil(-2.5) | ceiling(2.5) | +------------+--------------+ | -2 | 3 | +------------+--------------+ # 求余函数 mod(x,y) 返回 x 被 y 除后的余数 mysql> select mod(63,8),mod(120,10),mod(15.5,3); +-----------+-------------+-------------+ | mod(63,8) | mod(120,10) | mod(15.5,3) | +-----------+-------------+-------------+ | 7 | 0 | 0.5 | +-----------+-------------+-------------+ # rand() 函数被调用时,可以产生一个在 0 和 1 之间的随机数 mysql> select rand(), rand(), rand(); +---------------------+--------------------+----------------------+ | rand() | rand() | rand() | +---------------------+--------------------+----------------------+ | 0.24996517063115273 | 0.9559759106077029 | 0.029984071878701515 | +---------------------+--------------------+----------------------+
字符串函数可以对字符串类型数据进行处理,在程序应用中用处还是比较大的,同样这里列举几个常用的如下:
一些示例:
# length(str) 函数的返回值为字符串的字节长度 mysql> select length('name'),length('数据库'); +----------------+---------------------+ | length('name') | length('数据库') | +----------------+---------------------+ | 4 | 9 | +----------------+---------------------+ # concat(sl,s2,...) 函数返回结果为连接参数产生的字符串 若有任何一个参数为 null,则返回值为 null mysql> select concat('mysql','5.7'),concat('mysql',null); +-----------------------+----------------------+ | concat('mysql','5.7') | concat('mysql',null) | +-----------------------+----------------------+ | mysql5.7 | null | +-----------------------+----------------------+ # insert(s1,x,len,s2) 返回字符串 s1,子字符串起始于 x 位置,并且用 len 个字符长的字符串代替 s2 mysql> select insert('football',2,4,'play') as col1, -> insert('football',-1,4,'play') as col2, -> insert('football',3,20,'play') as col3; +----------+----------+--------+ | col1 | col2 | col3 | +----------+----------+--------+ | fplayall | football | foplay | +----------+----------+--------+ # upper,lower是大小写转换函数 mysql> select lower('blue'),lower('blue'),upper('green'),upper('green'); +---------------+---------------+----------------+----------------+ | lower('blue') | lower('blue') | upper('green') | upper('green') | +---------------+---------------+----------------+----------------+ | blue | blue | green | green | +---------------+---------------+----------------+----------------+ # left,right是截取左边或右边字符串函数 mysql> select left('mysql',2),right('mysql',3); +-----------------+------------------+ | left('mysql',2) | right('mysql',3) | +-----------------+------------------+ | my | sql | +-----------------+------------------+ # replace(s,s1,s2) 使用字符串 s2 替换字符串 s 中所有的字符串 s1 mysql> select replace('aaa.mysql.com','a','w'); +----------------------------------+ | replace('aaa.mysql.com','a','w') | +----------------------------------+ | www.mysql.com | +----------------------------------+ # 函数 substring(s,n,len) 带有 len 参数的格式,从字符串 s 返回一个长度同 len 字符相同的子字符串,起始于位置 n mysql> select substring('computer',3) as col1, -> substring('computer',3,4) as col2, -> substring('computer',-3) as col3, -> substring('computer',-5,3) as col4; +--------+------+------+------+ | col1 | col2 | col3 | col4 | +--------+------+------+------+ | mputer | mput | ter | put | +--------+------+------+------+
一些示例:
# curdate() 和 current_date() 函数的作用相同,将当前日期按照“yyyy-mm-dd”或“yyyymmdd”格式的值返回 mysql> select curdate(),current_date(),current_date()+0; +------------+----------------+------------------+ | curdate() | current_date() | current_date()+0 | +------------+----------------+------------------+ | 2019-10-22 | 2019-10-22 | 20191022 | +------------+----------------+------------------+ # month(date) 函数返回指定 date 对应的月份 mysql> select month('2017-12-15'); +---------------------+ | month('2017-12-15') | +---------------------+ | 12 | +---------------------+ # date_add(date,interval expr type) 和 adddate(date,interval expr type) 两个函数的作用相同,都是用于执行日期的加运算。 mysql> select date_add('2018-10-31 23:59:59',interval 1 second) as c1, -> date_add('2018-10-31 23:59:59',interval '1:1' minute_second) as c2, -> adddate('2018-10-31 23:59:59',interval 1 second) as c3; +---------------------+---------------------+---------------------+ | c1 | c2 | c3 | +---------------------+---------------------+---------------------+ | 2018-11-01 00:00:00 | 2018-11-01 00:01:00 | 2018-11-01 00:00:00 | +---------------------+---------------------+---------------------+ # datediff(date1,date2) 返回起始时间 date1 和结束时间 date2 之间的天数 mysql> select datediff('2017-11-30','2017-11-29') as col1, -> datediff('2017-11-30','2017-12-15') as col2; +------+------+ | col1 | col2 | +------+------+ | 1 | -15 | +------+------+ # date_format(date,format) 函数是根据 format 指定的格式显示 date 值 mysql> select date_format('2017-11-15 21:45:00','%w %m %d %y') as col1, -> date_format('2017-11-15 21:45:00','%h:i% %p %m %d %y') as col2; +------------------------------+----------------------------+ | col1 | col2 | +------------------------------+----------------------------+ | wednesday november 15th 2017 | 09:i pm november 15th 2017 | +------------------------------+----------------------------+
流程控制类函数可以进行条件操作,用来实现sql的条件逻辑,允许开发者将一些应用程序业务逻辑转换到数据库后台,列举如下:
一些示例:
# if 语句允许根据表达式的某个条件或值结果来执行一组 sql 语句 mysql> select if(1<2,1,0) c1,if(1>5,'√','×') c2,if(strcmp('abc','ab'),'yes','no') c3; +----+----+-----+ | c1 | c2 | c3 | +----+----+-----+ | 1 | × | yes | +----+----+-----+ # ifnull接受两个参数,如果第一个不是 null,则返回第一个参数。 否则,ifnull 函数返回第二个参数 mysql> select ifnull(5,8),ifnull(null,'ok'); +-------------+-------------------+ | ifnull(5,8) | ifnull(null,'ok') | +-------------+-------------------+ | 5 | ok | +-------------+-------------------+ # nullif函数 mysql> select nullif(5,8),nullif(8,8); +-------------+-------------+ | nullif(5,8) | nullif(8,8) | +-------------+-------------+ | 5 | null | +-------------+-------------+ # case when 函数 mysql> select case when 1>0 then 'true' else 'false' end; +--------------------------------------------+ | case when 1>0 then 'true' else 'false' end | +--------------------------------------------+ | true | +--------------------------------------------+ mysql> select case 11 when 1 then 'one' -> when 2 then 'two' else 'more' end; +-------------------------------------------------------------+ | case 11 when 1 then 'one' when 2 then 'two' else 'more' end | +-------------------------------------------------------------+ | more | +-------------------------------------------------------------+
加密函数主要用于对字符串进行加密,常用的几个列举如下:
一些示例:
# encrypt函数 mysql> select encrypt('root','salt'); +------------------------+ | encrypt('root','salt') | +------------------------+ | safkjij3elacw | +------------------------+ # md5()函数 mysql> select md5('123456'); +----------------------------------+ | md5('123456') | +----------------------------------+ | e10adc3949ba59abbe56e057f20f883e | +----------------------------------+ # password函数 mysql> select password('abcd'); +-------------------------------------------+ | password('abcd') | +-------------------------------------------+ | *a154c52565e9e7f94bfc08a1fe702624ed8effda | +-------------------------------------------+
总结:
本文主要介绍了mysql常用函数的作用,简单举例说明部分函数的用法。在实际环境中,这些函数可能嵌套使用,使用方法要复杂很多,希望大家用到的时候要多多注意各个参数的作用。
参考资料:
如对本文有疑问, 点击进行留言回复!!
一步步教你用Prometheus搭建实时监控系统系列(一)——上帝之火,普罗米修斯的崛起
网友评论