Mysql 常用函数
聚合函数:
count() 计数
sum() 求和 必须是数值类型
avg() 求平均 必须是数值类型
max() 最大值 可以 是字符类型
min() 最小值 可以 是字符类型
字符串函数:
charset( str ) 查看 字符串 的字符集
concat(str, str2) 将str2拼接到 str的后面
instr(string,substring) 返回 子字符串 在 父串中的位置,没有返回0
ucase(string) 转换成大写
lcase(string) 转换成小写
left(string,length) 从字符串左边开始,提取length个字符
length(string) 长度
replace(str,search_str,replace_str) 在str中用replace_str替换search_str
strcmp(string1,string2) 逐个字符比较两字符串的大小
substring(str,position [,length]) 从 第position个 开始截取 length个 字符,【position从1开始】
ltrim(string) 去除左空格
rtrim(string) 去除右空格
数学函数:
abs(number) 绝对值
bin(number) 十进制 转 二进制
hex(number) 转 十六进制
least(number1,number2 [,.....]) 求最小值
mod(number,denominator) 求余数,取模
rand( [seen] ) 随机数 rand()返回随即 0-1 的浮点数
ceiling(number) 向上取整
floor(number) 向下取整
format(number,decimal_places) 保留小数 位数
日期时间函数:
current_date() 当前日期 : 年-月-日
current_time() 当前时间: 时:分:秒
now() 当前日期时间:年-月-日 时:分:秒
year( current_date() ) 当前 年份
month( current_date()) 当期 月份
day( current_date()) 当前 月 的 第几天
hour( current_time()) 当前 小时
minute( current_time()) 当前 分钟
second( current_time()) 当前 秒
date( now()) 当前日期:年-月-日
time( now()) 当期时间:时:分:秒
date_add( date/datetime/timestamp, INTERVAL [+ | -] 时间值 date|month|day|hour|minute|second) 时间相加,在date上加上 vardate ,加负数为减法
date_sub( date/datetime/timestamp, INTERVAL [+ | -] 时间值 date|month|day|hour|minute|second) 时间相减,在date上减去 vardate ,减负数为加法
例如:select date_add( now(), INTERVAL 2 month) from dual; -- 当前日期 加 2月
select date_sub( now(), INTERVAL -2 month) from dual; -- 当前日期 加 2月
date_diff( date1,date2) 计算时间差 ,返回天数
time_diff( date1,date2) 计算时间差 ,返回 时:分:秒
其他函数:
加密函数:md5()