SQL 函数

86 阅读2分钟

SQL 函数

数字函数

PI()                        返回圆周率
truncate(x, y)              返回数值 x 保留到小数点后 y 位的值

字符串函数

char_length(str)                    返回字符串 str 的字符数
length(str)                         返回字符串 str 的长度
concat(str1, str2, str3, ...)       将字符串 str1, str2, str3 等多个字符串合并为一个字符串
concat_ws(separator, str1, str1,...)同COUCAT, 但是每个字符串之间要加上 separator
group_concat(字段 separator '分隔符')将字段通过分隔符连接为一个字符串
left(str,n)                         返回字符串 str 的前 n 个字符
right(str,n)                        返回字符串 str 的后 n 个字符
trim(str)                           去掉字符串 str 开始处和结尾处的空格
trim(substr from str)               去掉字符串 str 中开始处和结尾处的字符串 substr
locate(substr, str)                 返回字符串 substr 在字符串 str 中第一次出现的位置
instr(str, substr or pattern)       返回 substr 或模式 在 str 中的第一个位置
replace(str, s1, s2)                用字符串 s2 代替字符串 str 中的字符串 s1
substring(str, n, len)              获取从字符串 str 中的第 n 个位置开始长度为 len 的字符串
mid(s,n,len)                        同 substring(s,n,len)

日期时间函数

curdate()                   返回当前日期
current_date()              返回当前日期
localtimestamp()            返回当前日期和时间
date_format(d, f)           按照表达式 f 的要求显示日期 d
time_format(t, f)           按照表达式 f 的要求显示时间 t
datediff(d1, d2)            计算日期 d1 - d2 的天数
adddate(d, n)               计算日期 d 加上 n 天的日期
subdate(d, n)               计算日期 d 减去 n 天的日期
timestampdiff(unit, d1, d2) 计算两个日期 d2 - d1 指定单位(unit: second, minute, hour, day, week, month, year)的时间差