3.mysql 函数
函数类型
- 数学函数
- 字符串函数
- 日期和时间函数
- 条件判断函数
- 系统信息函数
- 加密函数
数学函数
- 绝对值 abs(x)
select abs(-1);
- 返回圆周率的函数 pi()
结果保留 7ww 有效数字
select pi();
- 平方根函数 sqrt(x)
select sqrt(4);
- 求余函数 mod(x, y)
select mod(5, 2);
- 取证函数 ceil(x), ceiling(x), floor(x)
# ceil 与 ceiling 一样. 向上取整
select ceil(3.1);
# floor 乡下取整
select floort(3.1);
- 随机数 rand(), rand(x)
# 不带参数的在一次查询中, 每个结果都不一样. 结果区间 [0,1.0]
select rand(), rand(), rand();
# 带参数的在一次查询中, 如果参数一样, 则结果也一样
select rand(10), rand(10), rand(11);
- 四舍五入函数 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 |
# +-------------------+
- 符号函数 sign(x)
# 负数为-1, 0为0, 正数为1.
select sign(-10), sign(0), sign(10);
# +-----------+---------+----------+
# | sign(-10) | sign(0) | sign(10) |
# +-----------+---------+----------+
# | -1 | 0 | 1 |
# +-----------+---------+----------+
- 幂函数 pow(x, n), power(x, n), exp(x)
# pow(x, n) 返回x的n次幂.
select pow(2,3);
# exp 特指 以e为底的指数
select exp(1);
- 对数函数 log(x), log10(x)
# 以 e 为底
select log(1);
# 以 10 为底
select log10(10);
- 角度与弧度转换 radians(x), degrees(x)
# 90度 = pi/2
select radians(90);
# pi = 180度
select degrees(pi());
- 三角函数 sin(x), asin(x), cos(x), acos(x), tan(x), atan(x), cot(x),
字符串函数
- 计算字符数和长度 char_length(s), length(s)
# 计算单子节字符时两者结果一致
select char_length('data');
select length('data');
- 合并字符串 concat(s1, s2), concat_ws(c,s1, s2)
select concat("Hello", " world");
# concat_ws, 可以指定额外的分隔符 ws => word separator
select concat_ws(" ", "Hello", "world");
- 替换字符串 insert(s1, x, len, s2)
# 将字符串 s1 中的起始 x 位置处开始 len 长度的部分替换成字符串 s2. 如果超出返回原字符串
select insert("Hello", 2, 4, "obby");
-
字母大小写转换 lower(s), lcase(s), upper(s), ucase(s)
-
获取指定长度的字符串 left(s,n), right(s,n)
-
填充字符串 lpad(s1, len, s2), rpad(s1, len, s2)
-
删除字符串的左右空格 ltrim(s1, s2), rtrim(s1, s2), trim(s)
-
删除指定字符串的 trim(s1 from s)
select trim("o" from "hello");
-
生成重复字符串 repeat(s, n)
-
空格函数 space(n)
-
字符串替换 replace(s, s1, s2)
-
字符串比较 strcmp(s1, s2)
-
获取字符串字串 substring(s, n, len), mid(s, n, len)
-
匹配字符串开始的位置 locate(s2, s1), position(s2 in s1), instr(s1, s2)
-
反转字符串 reverse(s)
-
获取指定位置的字符串 elt(n, s1, s2, s3, ..., sn)
-
获取指定字符串位置 field(s, s1, s2, ..., sn)
select field("Hi", "hihi", "hey", "Hi", "bas") as col;
-
获取子串位置 find_in_set(s1, s2)
-
选取字符串 make_set(x, s1, s2, ..., sn)
# 5 => 0101. 寻取"1"位
select make_set(5, "a", "b", "c", "d", "e") as col;
# +-----+
# | col |
# +-----+
# | a,c |
# +-----+
日期函数
- 获取当前日期 curdate(), current_date()
# 结果格式 YYYY-MM-DD
select curdate();
# curdate() +0. 返回仅数值型结果. 下同
- 获取当前时间 curtime(), current_time()
# 结果格式 YY-MM-SS
- 获取当前日期和时间 current_timestamp(), localtime(), now(), sysdate()
# 返回格式 YYYY-MM-DD HH:MM:SS
-
UNIX 时间戳 unix_timestamp(), unix_timestamp(s). from_unixtime(s)
-
utc 日期和时间 utc_data(), utc_time()
-
获取月份 month(d), mounthname(d)
-
获取星期 dayname(d), dayofweek(d), weekday(d)
-
获取星期数 week(d), weekofyear(d)
-
获取天数 dayofyear(d), dayofmonth(d)
-
获取年份 year(d)
-
获取季度 quarter(d)
-
获取小时 hour(d), 分钟 minute(d), 秒 second(d)
-
从给定日期截取 extract(type from date)
select extract(year from now());
-
时间和秒转换 time_to_sec(d), sec_to_time(s)
-
时间计算 date_add(d, interval), adddate(d, interval), date_sub(d, interval), subdate(d, interval)
-
计算时间差 datediff(d1, d2)
-
格式化 date_format(d, format)
条件判断函数
-
if(expr, v1, v2)
expr 若 true 为 v1, 否则 v2
-
ifnull(v1, v2) v1 不为 null 为 v1, 否则 v2
-
case expr when v1 then r1 [when v2 then r2] ... [else rn+1] END
系统信息函数
-
查看 mysql 版本 version()
-
查看当前连接数次数 connetcion_id()
-
查看正在运行的线程
show processlist;
show full processlist;
-
查看当前数据库 database(), schema()
-
获取用户名 user(), current_user(), system_user(), session_user()
-
获取字符串的字符集 charset("a"), 排序方式 collation("a")
-
获取最后一个自动生成的 id 值 last_insert_id()
加密函数
-
md5(s)
-
sha(s)
-
sha2(s, hash_length)
其他函数
-
格式化函数 format(x, n)
-
格式转换 conv(n, from_base, to_base)
select conv(10, 10, 2);
-
ip 与数字转换 inet_aton(ip), inet_ntoa(n)
-
加锁 get_lock(s) , 解锁 release_lock(s), 检查锁 if_free_lock(s), 状态 is_used_lock(s)
-
重复执行函数 benchmark(count, expr)
-
改变字符集 convert(s using type)
-
改变数据类型 cast(x, as type), convert(x, type)
-
窗口函数