函数类型

  1. 数学函数
  2. 字符串函数
  3. 日期和时间函数
  4. 条件判断函数
  5. 系统信息函数
  6. 加密函数

数学函数

  1. 绝对值 abs(x)
select abs(-1);
  1. 返回圆周率的函数 pi()

结果保留 7ww 有效数字

select pi();
  1. 平方根函数 sqrt(x)
select sqrt(4);
  1. 求余函数 mod(x, y)
select mod(5, 2);
  1. 取证函数 ceil(x), ceiling(x), floor(x)
# ceil 与 ceiling 一样. 向上取整
select ceil(3.1);
# floor 乡下取整
select floort(3.1);
  1. 随机数 rand(), rand(x)
# 不带参数的在一次查询中, 每个结果都不一样. 结果区间 [0,1.0]
select rand(), rand(), rand();
# 带参数的在一次查询中, 如果参数一样, 则结果也一样
select rand(10), rand(10), rand(11);
  1. 四舍五入函数 round(x), round(x, n), truncate(x,n)
# round(x) 保留整数
select round(2.5);
# round(x, n). 保留n位小数. n 如果为负数, 小数点左边对应位直接置零
select(12.525, 2);
# +------------------+
# | round(12.525, 2) |
# +------------------+
# |            12.53 |
# +------------------+
select(12.525, -1);
# +-------------------+
# | round(12.525, -1) |
# +-------------------+
# |                10 |
# +-------------------+

# truncate(x,n) 直接截断至n位. n 如果为负数, 小数点左边对应位直接置零
select truncate(1.95,1);
# +-------------------+
# | truncate(1.95, 1) |
# +-------------------+
# |               1.9 |
# +-------------------+
  1. 符号函数 sign(x)
# 负数为-1, 0为0, 正数为1.
select sign(-10), sign(0), sign(10);
# +-----------+---------+----------+
# | sign(-10) | sign(0) | sign(10) |
# +-----------+---------+----------+
# |        -1 |       0 |        1 |
# +-----------+---------+----------+
  1. 幂函数 pow(x, n), power(x, n), exp(x)
# pow(x, n) 返回x的n次幂.
select pow(2,3);
# exp 特指 以e为底的指数
select exp(1);
  1. 对数函数 log(x), log10(x)
# 以 e 为底
select log(1);
# 以 10 为底
select log10(10);
  1. 角度与弧度转换 radians(x), degrees(x)
# 90度 = pi/2
select radians(90);
# pi = 180度
select degrees(pi());
  1. 三角函数 sin(x), asin(x), cos(x), acos(x), tan(x), atan(x), cot(x),

字符串函数

  1. 计算字符数和长度 char_length(s), length(s)
# 计算单子节字符时两者结果一致
select char_length('data');
select length('data');
  1. 合并字符串 concat(s1, s2), concat_ws(c,s1, s2)
select concat("Hello", " world");
# concat_ws, 可以指定额外的分隔符 ws => word separator
select concat_ws(" ", "Hello", "world");
  1. 替换字符串 insert(s1, x, len, s2)
# 将字符串 s1 中的起始 x 位置处开始 len 长度的部分替换成字符串 s2. 如果超出返回原字符串
select insert("Hello", 2, 4, "obby");
  1. 字母大小写转换 lower(s), lcase(s), upper(s), ucase(s)

  2. 获取指定长度的字符串 left(s,n), right(s,n)

  3. 填充字符串 lpad(s1, len, s2), rpad(s1, len, s2)

  4. 删除字符串的左右空格 ltrim(s1, s2), rtrim(s1, s2), trim(s)

  5. 删除指定字符串的 trim(s1 from s)

select trim("o" from "hello");
  1. 生成重复字符串 repeat(s, n)

  2. 空格函数 space(n)

  3. 字符串替换 replace(s, s1, s2)

  4. 字符串比较 strcmp(s1, s2)

  5. 获取字符串字串 substring(s, n, len), mid(s, n, len)

  6. 匹配字符串开始的位置 locate(s2, s1), position(s2 in s1), instr(s1, s2)

  7. 反转字符串 reverse(s)

  8. 获取指定位置的字符串 elt(n, s1, s2, s3, ..., sn)

  9. 获取指定字符串位置 field(s, s1, s2, ..., sn)

select field("Hi", "hihi", "hey", "Hi", "bas") as col;
  1. 获取子串位置 find_in_set(s1, s2)

  2. 选取字符串 make_set(x, s1, s2, ..., sn)

# 5 => 0101. 寻取"1"位
select make_set(5, "a", "b", "c", "d", "e") as col;
# +-----+
# | col |
# +-----+
# | a,c |
# +-----+

日期函数

  1. 获取当前日期 curdate(), current_date()
# 结果格式 YYYY-MM-DD
select curdate();
# curdate() +0. 返回仅数值型结果. 下同
  1. 获取当前时间 curtime(), current_time()
# 结果格式 YY-MM-SS
  1. 获取当前日期和时间 current_timestamp(), localtime(), now(), sysdate()
# 返回格式 YYYY-MM-DD HH:MM:SS
  1. UNIX 时间戳 unix_timestamp(), unix_timestamp(s). from_unixtime(s)

  2. utc 日期和时间 utc_data(), utc_time()

  3. 获取月份 month(d), mounthname(d)

  4. 获取星期 dayname(d), dayofweek(d), weekday(d)

  5. 获取星期数 week(d), weekofyear(d)

  6. 获取天数 dayofyear(d), dayofmonth(d)

  7. 获取年份 year(d)

  8. 获取季度 quarter(d)

  9. 获取小时 hour(d), 分钟 minute(d), 秒 second(d)

  10. 从给定日期截取 extract(type from date)

select extract(year from now());
  1. 时间和秒转换 time_to_sec(d), sec_to_time(s)

  2. 时间计算 date_add(d, interval), adddate(d, interval), date_sub(d, interval), subdate(d, interval)

  3. 计算时间差 datediff(d1, d2)

  4. 格式化 date_format(d, format)

条件判断函数

  1. if(expr, v1, v2)

    expr 若 true 为 v1, 否则 v2

  2. ifnull(v1, v2) v1 不为 null 为 v1, 否则 v2

  3. case expr when v1 then r1 [when v2 then r2] ... [else rn+1] END

系统信息函数

  1. 查看 mysql 版本 version()

  2. 查看当前连接数次数 connetcion_id()

  3. 查看正在运行的线程

show processlist;
show full processlist;
  1. 查看当前数据库 database(), schema()

  2. 获取用户名 user(), current_user(), system_user(), session_user()

  3. 获取字符串的字符集 charset("a"), 排序方式 collation("a")

  4. 获取最后一个自动生成的 id 值 last_insert_id()

加密函数

  1. md5(s)

  2. sha(s)

  3. sha2(s, hash_length)

其他函数

  1. 格式化函数 format(x, n)

  2. 格式转换 conv(n, from_base, to_base)

select conv(10, 10, 2);
  1. ip 与数字转换 inet_aton(ip), inet_ntoa(n)

  2. 加锁 get_lock(s) , 解锁 release_lock(s), 检查锁 if_free_lock(s), 状态 is_used_lock(s)

  3. 重复执行函数 benchmark(count, expr)

  4. 改变字符集 convert(s using type)

  5. 改变数据类型 cast(x, as type), convert(x, type)

  6. 窗口函数