MySQL, Oracle, Linux, 软件架构及大数据技术知识分享平台

网站首页 > 精选文章 / 正文

MySQL常用函数集

2025-02-14 23:27 huorong 精选文章 2 ℃ 0 评论



一、字符串函数

1.1 拼接两个或多个字符串

SELECT CONCAT('hello', ' ', 'world'); # hello world

1.2 截取字符串

SELECT SUBSTRING('hello world', 1, 5); # hello

1.3 获取字符串的长度

SELECT length('hello world'); # 11

1.4 将字符串转为小写

select lower('HELLO WORLD'); # hello world

1.5 将字符串转为大写

select upper('hello world'); # HELLO WORLD

1.6 去除字符串首尾空格

select trim(' hello world '); # hello world

1.7 替换字符串内容

select replace('hello world', 'world', 'mysql'); # hello mysql

1.8 从字符串左边截取

select left('hello world', 5); # hello

1.9 从字符串右边截取

select right('hello world', 5); # world

1.10 从字符串左边填充字符

select lpad('hello', 10, '*'); # *****hello

1.11 从字符串右边填充字符

select rpad('hello', 10, '*'); # hello*****

1.12 反转字符串

select reverse('hello world'); # dlrow olleh

1.13 查询指定字符的位置

select instr('hello world', 'o'); # 5

1.14 查询字符串在集合中的位置

select find_in_set('mysql', 'oracle,mysql,sqlserver,postgresql'); # 2

二、数学函数

2.1 绝对值

select abs(-10); # 10

2.2 向上取整

select ceil(0.1); # 1
select ceil(0.9); # 1

2.3 向下取整

select floor(0.1); # 0
select floor(0.9); # 0

## 2.4 四舍五入

select round(0.1); # 0
select round(0.9); # 1

2.5 求余

select mod(10, 3); # 1

2.6 幂

select pow(2, 3); # 8

2.7 e的指定次幂

select exp(2); # 7.38905609893065

2.8 指定对数的自然对数

select log(2); # 0.693147180559945

2.9 指定对数的以10为底的对数

select log10(2); # 0.301029995663981

2.10 指定对数的以2为底的对数

select log2(4); # 2

2.11 平方根

select sqrt(4); # 2

2.12 随机数

select rand();

2.13 列表中的最大值

select greatest(1, 2, 3, 4, 5);

2.14 列表中的最小值

select least(1, 2, 3, 4, 5);

三、日期 和 时间 函数

3.1 当前时间和日期

select now();

3.2 当前日期

select CURDATE();

3.3 当前时间

select curtime();

3.4 添加时间

select date_add('2020-01-01', interval 1 day); # 2020-01-02

3.5 减去时间

select date_sub('2020-01-01', interval 1 day); # 2019-12-31

3.6 计算两个日期之差

select DATEDIFF('2020-01-01', '2020-01-02'); # -1

3.7 日期格式化

select date_format('2020-01-01', '%Y-%m-%d %H:%i:%s');

3.8 获取日期中的天

select day('2020-01-21'); # 21

3.9 获取日期中的月

select month('2020-01-21'); # 1

3.10 获取日期中的年

select year('2020-01-21'); # 2020

3.11 获取时间中的小时

select hour('2020-01-21 12:00:00'); # 12

3.12 获取时间中的分钟

select minute('2020-01-21 12:30:10'); # 30

3.13 获取时间中的秒

select second('2020-01-21 12:30:10'); # 10

3.14 两个日期之间的时间差

select timestampdiff(day, '2020-01-01', '2020-01-02');
select timestampdiff(hour, '2020-01-01', '2020-01-02 12:00:00');

3.15 字符串转为日期

select str_to_date('2020-01-01', '%Y-%m-%d'); # 2020-01-01

四、聚合函数

4.1 计算行数

select count(1) from dual;

4.2 求和

select sum(money) from dual;

4.3 平均值

select avg(money) from dual;

4.4 最大值

select max(money) from dual;

4.5 最小值

select min(money) from dual;

4.6 将组内的值连接成一个字符串

select group_concat(name) from dual group by name;

4.7 方差

select variance(money) from dual;

4.8 标准差

select stddev(money) from dual;

五、条件判断函数

5.1 if()

select if(1=1, 'true', 'false'); # true

5.2 case

select case 1 when 1 then 'true' else 'false' end; # true

5.2 coalese 返回第一个非空值

select coalesce(null, null, 'hello', 'world'); # hello

5.3 如果两个表达式相等,返回null

select nullif(1, 1); # null
select nullif(1, 2); # 1

5.4 如果第一个参数为null,则返回第二个参数

select ifnull(null, 'hello'); # hello
select ifnull(1, 'hello'); # 1

六、加解密

6.1 SHA

select sha('hello'); # aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
select sha1('hello'); # aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
select sha2('hello', 256); # 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

6.2 MD5

select md5('hello'); # 5d41402abc4b2a76b9719d911017c592

6.3 AES

select aes_encrypt('hello', 'ijunfu'); # 4ed42423-9d5e-38c7-0172-64494c5eacf2
select aes_decrypt(aes_encrypt('hello', 'ijunfu'), 'ijunfu');

七、JSON

7.1 创建JSON数组

select json_array('hello', 'world');

7.2 创建JSON对象

select json_object('name', 'hello', 'age', 18);

7.3 获取JSON数组中的值

select json_extract('["hello", "world"]', '$[1]'); # world
select json_extract('{"name": "hello", "age": 18}', '$."name"'); # hello

7.4 合并JSON

select json_merge('{"name": "hello"}', '{"age": 18}');

7.5 取消JSON值的引号

select json_unquote('{"name": "hello", "age": 18}');

八、其他函数

8.1 UUID

select uuid();

8.2 将IP地址转为数字

select inet_aton('192.168.1.1');
select inet6_aton('ABCD:EF01:2345:6789:ABCD:EF01:2345:6789');

8.2 将数字转为IP地址

select inet_ntoa(inet_aton('192.168.1.1'));
select inet6_ntoa(inet6_aton('ABCD:EF01:2345:6789:ABCD:EF01:2345:6789'));

8.3 休眠

select sleep(1);

Tags:mysql 绝对值

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言