MYSQL常用函数大全(个人总结)

# 使用test数据库
use test;
# 创建学生表
drop table if exists student;
create table student
(
    id   int         null comment '编号',
    name varchar(20) null comment '姓名',
    age  int         null comment '年龄',
    sex  varchar(20) null comment '性别'
);
INSERT INTO test.student (id, name, age, sex) VALUES (1, 'TOM', 20, '男');
INSERT INTO test.student (id, name, age, sex) VALUES (2, 'TONG', 30, '男');
INSERT INTO test.student (id, name, age, sex) VALUES (3, 'LIU', 22, '女');
INSERT INTO test.student (id, name, age, sex) VALUES (4, 'XIAO', 33, '男');

一.聚合函数

# 查询学生表年龄最大的
select max(age) from student;
# 查询学生表年龄最小的
select min(age) from student;
# 查询学生表年龄平均值
select avg(age) from student;
# 查看学生表年龄之和
select sum(age) from student;
# 查看学生表总条数
select count(1) from student;

二.字符串函数

# 查询学生表姓名的长度
select length(name) from student;
# 查看学生表姓名和性别合并
select concat(name,sex) from student;
# 将字符串大写
select upper(name) from student;
# 将字符串小写
select lower(name) from student;
# 查看将学生表名字为TOM的替换成ROM
select insert(name,1,1,'R') from student where name='TOM';
# 查看将学生表名字为TOM的前两个字符
select left(name,2) from student where name='TOM';
# 查看将学生表名字为TOM的后两个字符
select right(name,2) from student where name='TOM';
# 截取字段串
select substring('liqing',2,4);
# 字符串替换  将'qing'替换成'yao'
select replace('liqing','qing','yao');
# 清除左右两端空格
select trim('   Tom   ');
# 清除左端空格
select ltrim('   Tom   ');
# 清除右端空格
select rtrim('   Tom   ');
# 将字符串从右向左显示
select reverse('TOMAVSHDJ');

三.数值函数

# 求绝对值
select abs(-20);
# 求平方根
select sqrt(25);
# 求余数
select mod(25,6);
# 返回正大整数
select ceil(3.5);
# 返回正小整数
select floor(3.5);
# 四舍五入
select round(3.5);
# 正数返回1,0返回0,负数返回-1
select sign(35);
# 返回6的3次方
select pow(6,3);

四.日期函数

# 返回系统当前日期(YYYY-MM-DD格式)
select curdate();
# 返回系统当前时间(HH24:MI:SS格式)
select curtime();
# 返回系统日期和时间
select now();
select sysdate();
# 返回一个以UNIX时间戳为基础的无符号整数
select UNIX_TIMESTAMP(DATE_FORMAT('2022-01-08 10:08:24','%Y-%m-%d %H:%m:%s'));
select UNIX_TIMESTAMP(NOW());
# 输入UNIX_TIMESTAMP函数的值,返回日期
SELECT FROM_UNIXTIME('1644546572');
# 获取日期中的月份
select month(now());
# 获取日期中的月份英明名称
select monthname(now());
# 获取日期中的天数
select day(now());
# 获取日期中的星期几英文
select dayname(now());
# 获取日期中年份
select year(sysdate());
# 将时间转换为以秒为单位
select time_to_sec(now());
# 将秒转换为时间
select sec_to_time('38159');
# 向日期添加指定的时间间
SELECT DATE_ADD('2021-10-31 23:59:59',INTERVAL 1 SECOND);   #加一秒
SELECT DATE_ADD('2021-10-31 23:59:59',INTERVAL 1 MINUTE);   #加一分钟
# 向日期减去指定的时间间
SELECT DATE_SUB('2022-01-02',INTERVAL 31 DAY) ;  #减三十一天
SELECT DATE_SUB('2022-01-02',INTERVAL 2 month ) ;  #减二月
# 时间加法运算,在原始时间上添加指定的时间
SELECT ADDTIME(now(),'2:3:1'),now();
SELECT ADDTIME('12:00:00','5:5:5');
#时间减法运算,在原始时间上减去指定的时间
SELECT SUBTIME(now(),'2:3:1'),now();
SELECT SUBTIME('12:00:00','5:5:5');
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值