MySQL数据库查询——内置函数

  • Post author:
  • Post category:mysql




一、单行函数


1、字符串函数

  • length(column_name|str)

    返回字符串存储长度,参数列表内为列名或直接是字符串
select length(name) from student

查询结果:查出name列中各条信息的字符串存储长度

  • char_length(column_name|str)

    返回字符串字符个数,参数列表内为列名或直接是字符串
select length(name) from student

查询结果:查出name列中各条信息的字符串中字符个数

  • concat(column_name1|str1, column_name2|str2,…)

    将多个字符串首尾相连后返回,参数列表内为列名或直接是字符串
select concat(id,name,sex) from student

查询结果:将id列,name列,sex列中信息首尾相连返回在同一列中

  • concat_ws(separator,column_name1|str1, column_name2|str2,…)

    将多个字符串以separator为间隔相连,separator为自己设置的间隔符号,之后的参数为列名或直接是字符串
select concat_ws(",",id,name,sex) from student

查询结果:将id列,name列,sex列中信息以”,”为间隔连接,返回在同一列中

  • trim([{both | leading | trailing} [remstr] from] str)

    返回去掉str源字符串两端、前缀或后缀字符串。其中both,leading,trailing分别代表两端,前缀,后缀。remstr为要去掉的字符,若不指定remstr则默认为空格,str为待操作字符串。
select trim('  Tom  ') from dual;
#执行结果字符串变为Tom
select trim(both 'a' from 'aaaTomaaa');
#执行结果字符串变为Tom
select trim(leading 'a' from 'aaaTom');
#执行结果字符串变为Tom
select trim(trailing 'abc' from 'Tomabc');
  • substr(str,pos,len)

    从源字符串str中的指定位置pos开始取len长度字串并返回,len缺省时一直取到字符串的末尾
select substr('beijing','3','2') from dual

查询结果:返回字符串ij

  • replace(str, from_str, to_str)

    将源字符串str中所有子串form_str(大小写敏感替)代成字符串to_str并返回替换后的字符串。
select replace('prefix.mysql.com','prefix','www');

查询结果:返回字符串

www.mysql.com

  • reverse(str)

    返回字符串str反转结果
  select reverse('123456');

查询结果:返回字符串654321

  • strcmp(expr1,expr2)

    两个字符串相同则返回0;第一个小于第二个返回-1,否则返回1;
select strcmp(10,11) from dual
select strcmp(11,11) from dual
select strcmp(11,10) from dual

查询结果:分别返回-1,0,1


2、数值函数

  • mod(x,y)

    取x/y的余数
select mod(2,3) from dual

查询结果:返回2

  • round(x,y)

    返回参数x的四舍五入值,该值有y位小数,y缺省时为0。
select round(4.516,2),round(4.168,1),round(4.168)from dual

查询结果:返回3列,结果分别为4.52,4.2,4

  • truncate(x,y)

    返回数字x截断后的结果,该值有y位小数
select truncate(1.58,0),truncate(1.298,1)

查询结果:返回2列,结果分别为1,1.2


3、日期函数

  • now()

    获得当前日期+时间
select now()

查询结果:2019-05-10 15:38:54

  • date_format(date,format)

    获取指定格式的日期
select date_format(now(),'%Y-%m-%d-%h-%i-%s')

查询结果:2019-05-10-03-40-11

  • datediff(date1,date2)

    返回(date1减date2)天
select datediff(now(),'2018-1-1')

查询结果:494

  • timediff(time1,time2)

    返回time1-time2,且函数的两个参数类型必须相同
select timediff(now(),'2019-05-10 00:00:00')

查询结果:15:44:04


4、转换函数

  • convert(value,type)

    将value转换为type类型,type可以是char(字符型)、date(日期型)、time(时间型)、datetime(日期时间型)、 signed(整型) 和decimal(浮点型)类型中的一个。
select convert(now(),char(10));  
#查询结果2019-05-10
select convert('99',signed)+1;
#查询结果100
select convert(now(),date);  
#查询结果2019-05-10
select convert('99.99',decimal(5,2));
#查询结果100.00
select convert(now(),time);
#查询结果15:51:19
select convert(now(),datetime);
#查询结果2019-05-10 15:53:16


5、其他函数

  • if(expr1,expr2,expr3)

    expr1为TRUE,返回expr2,否则返回expr3
select IF(name is null,'未知',name),name from student1

查询结果:

在这里插入图片描述

  • ifnull(expr1,expr2)

    expr1不是NULL,返回expr1,否则返回expr2。
 select ifnull(name,'未知'),name from student1

查询结果:

在这里插入图片描述



二、多行函数

  • avg(input)

    avg 函数返回数值列的平均值。NULL 值不包括在计算中。
 select avg(age) from student

查询结果:计算学生平均年龄

  • max(input)
select max(age) from student

查询结果:查询学生中年龄的最大值

  • min(input)
select min(age) from student

查询结果:查询学生中年龄的最小值

  • sum(input)
select sum(age) from student

查询结果:查询学生年龄总和

  • count(*|input)
select count(name) from student
#查询有几条学生姓名不为空的信息
select count(*) from student
#查询表中共有几条信息
  • stddev(input):求标准差
  • variance(input):求方差

注意:后两种函数一般不用



版权声明:本文为Zzze0101原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。