聚合函数
| 函数 | 说明 |
|---|---|
| count([distinct] 待统计列名称) | 返回查询到的数据的 数量 |
| sum([distinct] 待统计列名称) | 返回查询到的数据的 总和,不是数字没有意义 |
| avg([distinct] 待统计列名称) | 返回查询到的数据的 平均值,不是数字没有意义 |
| max([distinct] 待统计列名称) | 返回查询到的数据的 最大值,不是数字没有意义 |
| min([distinct] 待统计列名称) | 返回查询到的数据的 最小值,不是数字没有意义 |
- count
可以得到指定字段有多少行数据

- sum
得到指定字段中,数据的总和

- avg,得到指定字段的平均数值

以浮点数的方式显示出来
- min,max。显示指定字段中最大最小的数据

日期函数
| 函数名称 | 描述 |
|---|---|
| current_date() | 返回当前日期 |
| current_time() | 返回当前时间 |
| current_timestamp() | 返回当前时间戳 |
| date(datetime) | 返回datetime中的日期部分 |
| date_add(date,interval d_value_type) | 给date日期,加上 d_value_type,数据的单位可以是:year minute second day |
| date_sub(date,interval d_value_type) | 给date日期,减去 d_value_type,数据的单位可以是:year minute second day |
| datediff(date1,date2) | 返回两个日期的差,单位是天 |
| now() | 返回当前的日期 |
current_date(),得到当前日期

current_time(),得到当前时间

current_timestamp(),当前时间戳

date(datetime),返回日期部分

date_add(date,interval d_value_type),date_sub(date,interval d_value_type),日期的加减法

datediff(date1,date2),返回两个日期的差值(注意日期的格式)

now(),返回当前时间戳

字符串函数
| 函数 | 描述 |
|---|---|
| charset(str) | 返回字符串的字符集 |
| concat(str1,str2…) | 连接字符串 |
| instr(string, substring) | 返回 substring 在 string 中出现的位置,没有返回0 |
| ucase(str) | 字符串str 转为 大写 |
| lcase(str) | 字符串str转为小写 |
| left(str, length) | 从str字符串的左边取 length长度的字符 |
| length(str) | 求字符串长度 |
| replace(str, search_str,replace_str) | 用 replace_str 替换 str 中的 search_str |
| strcmp(str1,str2) | 按ASCII码 比较字符串大小 |
| substring(str, pos [,length]) | 截取字符串 pos 位置开始,length长度的字符串 |
| ltrim(str) rtrim(str) | 去除前后空格 |
- charset(str),返回字符串的字符集

- concat(str1,str2…),连接字符串

- instr(str, substr),字符串查找

- ucase,lcase,大小写转换

- substring,字符串截取

数学函数
| 函数 | 描述 |
|---|---|
| abs(num) | 绝对值函数 |
| bin(decimal_num) | 十进制转为二进制 |
| conv(num, from_base,to_base) | 进制转换 |
| ceiling(num) | 向上取整 |
| floor(num) | 向下取整 |
| format(num,decimal_place) | 格式化,保留几位小数 |
| hex(decimal_num) | 转为16进制数 |
| rand() | 随机一个0-1之间的浮点数 |
| mod(num, denominator) | 求模,取余 |
- format(num,decimal_place),格式化

- hex(num),转为16进制数

其他函数
| 函数 | 描述 |
|---|---|
| user() | 查询当前用户 |
| md5(str) | 返回字符串进行md5加密后的结果 |
| database() | 显示当前数据库 |
| password() | 对该用户进行加密 |
| ifnull(val1, val2) | 判断val1是否为空,不为空则返回val1,为空则返回val2 |
- md5(str),md5加密

- password(),对用户进行加密

本文全面介绍了MySQL中的各种内置函数,包括聚合函数、日期函数、字符串函数、数学函数及其他函数。详细解释了每个函数的功能和使用场景,如count、sum、avg等聚合函数用于统计数据,current_date、now等日期函数用于操作日期时间,以及concat、instr等字符串函数用于处理字符串。
1015

被折叠的 条评论
为什么被折叠?



