参考
round(DOUBLE a) 或 round(DOUBLE a, INT d)
round(DOUBLE a)
round(DOUBLE a, INT d)
select round(314.15926); select round(314.15926, 2); select round(314.15926, -2);
bround(DOUBLE a) 或 bround(DOUBLE a, INT d)
bround(DOUBLE a)
bround(DOUBLE a, INT d)
与普通四舍五入的区别:
这么设计的目的是:
select round(1.4), round(2.4), round(3.4), round(4.4); select round(1.5), round(2.5), round(3.5), round(4.5); select bround(1.4), bround(2.4), bround(3.4), bround(4.4); select bround(1.5), bround(2.5), bround(3.5), bround(4.5); select bround(1.6), bround(2.6), bround(3.6), bround(4.6);
floor(DOUBLE a)
ceil(DOUBLE a)
ceiling(DOUBLE a)
trunc(N,D)
trunc(date, fmt)
select floor(3.1415926), ceil(3.1415926), ceiling(3.1415926); select trunc(314.15926), trunc(314.15926, 1), trunc(314.15926, -1);
rand()
rand(INT seed)
select rand(), rand(), rand(), rand(), rand(); select rand(7), rand(7), rand(7), rand(7), rand(7);
abs(DOUBLE a)
positive(INT a)
positive(DOUBLE a)
+a
negative(INT a)
negative(DOUBLE a)
-a
sign(DOUBLE a)
sign(DECIMAL a)
select abs(-5), abs(0), abs(1.2); select positive(-3), positive(0), positive(2.1); select negative(-1), negative(0), negative(7); select sign(-10), sign(0), sign(200);
e()
pi()
pow(DOUBLE a, DOUBLE p)
power(DOUBLE a, DOUBLE p)
a
p
sqrt(DOUBLE a)
sqrt(DECIMAL a)
cbrt(DOUBLE a)
log10(DOUBLE a)
log10(DECIMAL a)
log2(DOUBLE a)
log2(DECIMAL a)
log(DOUBLE base, DOUBLE a)
log(DECIMAL base, DECIMAL a)
select pow(2, 2), power(3, 3); select sqrt(4), cbrt(27); select log10(100), log2(64); select log(3, 27);
exp(DOUBLE a)
exp(DECIMAL a)
ln(DOUBLE a)
ln(DECIMAL a)
bin(BIGINT a)
hex(BIGINT a)
hex(BINARY a)
将 BINARY 转换为十六进制字符串(返回 String)
这个没查到用法(BINARY 并不是指二进制数值,而是类似于 byte[] 字节序列,所以使用效果有点像 String )
byte[]
select hex(1010); -- 这个执行的是 hex(BIGINT a) select hex(conv(7,10,2)); -- 这个执行的是 hex(STRING a) -- select hex(0x1010); -- 这个会报错 select hex('0x1010'); -- 这个执行的是 hex(STRING a) select unhex(3030),unhex('3030'); select hex(unhex(3030)); -- 这个执行的是 hex(STRING a) -- 使用表中的 BINARY 类型字段也不行 drop table if exists test.test; create table IF NOT EXISTS test.test( bin BINARY ); insert into test.test values (1001), ('1010'), -- (0x1011), --报错 ('0x1100'); desc test.test; select bin, hex(bin) from test.test;
hex(STRING a)
unhex(STRING a)
?
hex(STRING a) 和 unhex(STRING a) 应该是互为相反的操作
conv(BIGINT num, INT from_base, INT to_base) 或 conv(STRING num, INT from_base, INT to_base)
conv(BIGINT num, INT from_base, INT to_base)
conv(STRING num, INT from_base, INT to_base)
将 BIGINT 或 STRING 从指定的原进制,转换为目标进制(返回 String)
select conv(2, 10, 2), conv('10', 10, 8), conv(12, 10, 16);
pmod(INT a, INT b) 或 pmod(DOUBLE a, DOUBLE b)
pmod(INT a, INT b)
pmod(DOUBLE a, DOUBLE b)
和取模运算符 a % b 效果一样
a % b
a 除以 b 取余数
factorial(INT a)
支持范围:[0, 20]
shiftleft(TINYINT|SMALLINT|INT|BIGINT a, INT b)
shiftright(TINYINT|SMALLINT|INT|BIGINT a, INT b)
shiftrightunsigned(TINYINT|SMALLINT|INT|BIGINT a, INT b)
select shiftleft(2, 1), shiftright(2, 1); select shiftleft(-2, 1), shiftright(-2, 1); select shiftrightunsigned(2, 1), shiftrightunsigned(-2, 1);
greatest(T v1, T v2, ...)
least(T v1, T v2, ...)
select greatest(1, 7, 2), least(1, 7, 2);
sin(DOUBLE a)
sin(DECIMAL a)
asin(DOUBLE a)
asin(DECIMAL a)
cos(DOUBLE a)
cos(DECIMAL a)
acos(DOUBLE a)
acos(DECIMAL a)
tan(DOUBLE a)
tan(DECIMAL a)
atan(DOUBLE a)
atan(DECIMAL a)
degrees(DOUBLE a)
degrees(DECIMAL a)
radians(DOUBLE a)
width_bucket(NUMERIC expr, NUMERIC min_value, NUMERIC max_value, INT num_buckets)