mysql之常用函数
- mysql函数的介绍
- 聚集函数
- avg --- select avg(xxx) as "xxx" from schema.table where xxx='xxx'; # 平均值
- count --- select count (*) as "xxx" from schema.table where xxx='xxx'; # 统计个数
- max --- select max(xxx) as "xxx" from schema.table where xxx='xxx'; # 最大值
- min --- select min(xxx) as "xxx" from schema.table where xxx='xxx'; # 最小值
- sum --- select sum(xxx) as "xxx" from schema.table where xxx='xxx'; # 求和
- distinct --- select count (distinct xxx) as "xxx" from schema.table where xxx='xxx'; # 在计算过程中会有重复值出现的话,distinct会将数据去重
- 用于处理字符串的函数
- 合并字符串函数:concat(str1,str2,str3…) # 合并字符串直接拼接
- 合并字符串函数:concat_ws(str1,str2,str3…) # 合并字符串带分隔符
- 比较字符串大小函数:strcmp(str1,str2) #比较两个字符串的大小。左大于右时返回1,左等于右时返回0,,左小于于右时返回-1,
- 获取字符串字节数函数: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)