本文实例讲述了mysql常用函数。分享给大家供大家参考,具体如下:
本文内容:
- mysql函数的介绍
- 聚集函数
- 用于处理字符串的函数
- 合并字符串函数:concat(str1,str2,str3…)
- 比较字符串大小函数:strcmp(str1,str2)
- 获取字符串字节数函数:length(str)
- 获取字符串字符数函数:char_length(str)
- 字母大小写转换函数:大写:upper(x),ucase(x);小写lower(x),lcase(x)
- 字符串查找函数
- 获取指定位置的子串
- 字符串去空函数
- 字符串替换函数:
- 用于处理数值的函数
- 绝对值函数:abs(x)
- 向上取整函数:ceil(x)
- 向下取整函数:floor(x)
- 取模函数:mod(x,y)
- 随机数函数:rand()
- 四舍五入函数:round(x,y)
- 数值截取函数:truncate(x,y)
- 用于处理时间日期的函数
- 获取当前日期:curdate(),current_date()
- 获取当前时间:curtime(),current_time()
- 获取当前日期时间:now()
- 从日期中选择出月份数:month(date),monthname(date)
- 从日期中选择出周数:week(date)
- 从日期中选择出周数:year(date)
- 从时间中选择出小时数:hour(time)
- 从时间中选择出分钟数:minute(time)
- 从时间中选择出今天是周几:weekday(date),dayname(date)
首发日期:2018-04-14
mysql函数的介绍:
- 为了简化操作,mysql提供了大量的函数给程序员使用(比如你想输入当前时间,可以调用now()函数)
- 函数可以出现的位置:插入语句的values()中,更新语句中,删除语句中,查询语句及其子句中。
聚集函数:
- 聚集函数用于汇集记录(比如不想知道每条学生记录的确切信息,只想知道学生记录数量,可以使用count())。
- 聚集函数就是用来处理“汇集数据”的,不要求了解详细的记录信息。
- 聚集函数(aggregate function) 运行在行组上,计算和返回单个值的函数。
实验表数据(下面的运行数据基于这个表):
create table student(
name varchar(15),
gender varchar(15),
age int
);
insert into student values("lilei","male",18);
insert into student values("alex","male",17);
insert into student values("jack","male",20);
insert into student values("john","male",19);
insert into student values("nullpeople","male",null);
avg(字段)函数:
- 返回指定字段的数据的平均值
- avg() 通过对表中行数计数并计算指定字段的数据总和,求得该字段的平均值。

- avg() 函数忽略列值为 NULL 的行,如果某行指定字段为null,那么不算这一行。
count(字段)函数:
- 返回指定字段的数据的行数(记录的数量)
- 字段可以为"*",为*时代表所有记录数,与字段数不同的时,记录数包括某些字段为null的记录,而字段数不包括为null的记录。


max(字段)函数:
- 返回指定字段的数据的最大值

- 如果指定字段的数据类型为字符串类型,先按字符串比较,然后返回最大值。
- max() 函数忽略列值为 null的行
min(字段)函数:
- 返回指定字段的数据的最小值

- 如果指定字段的数据类型为字符串类型,先按字符串比较,然后返回最小值。
- min()函数忽略列值为 null的行
sum(字段)函数:
- 返回指定字段的数据之和

- sum()函数忽略列值为 null的行
补充:
- 聚集函数的字段如果的数据为null,则忽略值为null的记录。
- 比如avg:有5行,但是只有四行的年龄数据,计算结果只算四行的,
- 但是如果不针对字段,那么会计算,比如count(x)是计算记录数的,null值不影响结果。
- 还有一些标准偏差聚集函数,这里不讲述,想了解更多的可以百度。
- 聚集函数在5.0+版本上还有一个选项DISTINCT,与select中类似,就是忽视同样的字段。【不可用于count(x)】
用于处理字符串的函数:
合并字符串函数:concat(str1,str2,str3…)
- 用于将多个字符串合并成一个字符串,如果传入的值中有null,那么最终结果是null
- 如果想要在多个字符串合并结果中将每个字符串都分隔一下,可以使用concat_ws(分隔符,str1,str2,str3…),如果传入的分隔符为null,那么最终结果是null(不过这时候如果str有为null不影响结果)

比较字符串大小函数:strcmp(str1,str2)
- 用于比较两个字符串的大小。左大于右时返回1,左等于右时返回0,,左小于于右时返回-1,
- strcmp类似编程语言中的比较字符串函数(依据ascll码?),会从左到右逐个比较,直到有一个不等就返回结果,否则比较到结尾。

获取字符串字节数函数:length(str)
- 用于获取字符串字节长度(返回字节数,因此要注意字符集)

获取字符串字符数函数:char_length(str)
- 用于获取字符串长度

字母大小写转换函数:大写:upper(x),ucase(x);小写lower(x),lcase(x)
- upper(x),ucase(x)用于将字母转成大写,x可以是单个字母也可以是字符串

- lower(x),lcase(x)用于将字母转成小写,x可以是单个字母也可以是字符串

- 对于已经是了的,不会进行大小写转换。
字符串查找函数:
- find_in_set(str1,str2)
- 返回字符串str1在str2中的位置,str2包含若干个以逗号分隔的字符串(可以把str2看出一个列表,元素是多个字符串,查找结果是str1在str2这个列表中的索引位置,从1开始)

- field(str,str1,str2,str3…)
- 与find_in_set类似,但str2由一个类似列表的字符串变成了多个字符串,返回str在str1,str2,str3…中的位置。

- locate(str1,str2):
- 返回子串str1在字符串str2中的位置

- position(str1 IN str2)
- 返回子串str1在字符串str2中的位置

- instr(str1,str2)
- 返回子串str2在字符串str1中的位置【注意这里调转了】

获取指定位置的子串:
- elt(index,str1,str2,str3…)
- 返回指定index位置的字符串

- left(str,n)
- 截取str左边n个字符

- right(str,n)
- 截取str右边n个字符

- substring(str,index,len)
- 从str的index位置截取len个字符

字符串去空函数:
- ltrim(str):
- 去除字符串str左边的空格

- rtrim(str)
- 去除字符串str右边的空格

- trim()
- 去除字符串str两边的空格

字符串替换函数:
- insert(str1,index,len,str2)
- 使用str2从str1的index位置替换str1的len个元素

- replace(str,str1,str2)
- 将str中的子串str1全部替换成str2

用于处理数值的函数:
绝对值函数:abs(x)
向上取整函数:ceil(x)
向下取整函数:floor(x)
取模函数:mod(x,y)
随机数函数:rand()
- 返回0-1内的随机数
- 如果想对某种情况都使用同一随机值,可以使用rand(x),x相同时返回同样的随机结果。

四舍五入函数:round(x,y)
- 返回数值x带有y为小数结果的数值(四舍五入)

数值截取函数:truncate(x,y)
- 返回数值x截取y位小数的结果(不四舍五入)

用于处理时间日期的函数:
获取当前日期:curdate(),current_date()
- 返回格式为:

获取当前时间:curtime(),current_time()
- 返回格式为:

获取当前日期时间:now()
- 返回格式为:

从日期中选择出月份数:month(date),monthname(date)
从日期中选择出周数:week(date)
- 返回格式为:

从日期中选择出周数:year(date)
- 返回格式为:

从时间中选择出小时数:hour(time)
- 返回格式为:

从时间中选择出分钟数:minute(time)
- 返回格式为:

从时间中选择出今天是周几:weekday(date),dayname(date)
- 返回格式为:

日期函数还是比较常用的,想了解更多,可以参考官方文档:
https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
想了解更多函数,可以参考官方文档(下面的是5.7的):
https://dev.mysql.com/doc/refman/5.7/en/func-op-summary-ref.html
更多关于MySQL相关内容感兴趣的读者可查看本站专题:《MySQL常用函数大汇总》、《MySQL日志操作技巧大全》、《MySQL事务操作技巧汇总》、《MySQL存储过程技巧大全》及《MySQL数据库锁相关技巧汇总》
希望本文所述对大家MySQL数据库计有所帮助。