MySQL8的官方文档:https://dev.mysql.com/doc/refman/8.0/en/
常用函数
-- ======数学运算========
select ABS(-23) as '绝对值';
select CEILING(9.6) as '向上取整';
select FLOOR(9.6) as '向下取整';
select RAND() as '0-1之间的随机数';
select SIGN(14) -- 0返回0 正数返回1 负数返回-1
-- 字符串函数
select CHAR_LENGTH('fhajdshfjkahfjah') as '字符串长度';
select CONCAT('心','如','止水') as '拼接字符串';
select UPPER('dfads') as '转大写';
select LOWER('AFSAFSA') as '转小写';
select REPLACE('人是大自然放的屁','屁','空气') as '替换后:';
-- =======时间周期函数========
select CURRENT_TIME() as '仅时间没有日期';
select CURRENT_DATE() as '现在的日期,没有时间';
select now() as '现在的时间包括日期、时间';
-- =====系统=====
select SYSTEM_USER(); -- 查看系统用户
select USER();
聚合函数(常用)
函数名 | 描述 |
---|---|
Count() | 计数 |
SUM() | 求和 |
AVG() | 求平均值 |
MAX() | 最大值 |
MIN() | 最小值 |
.... | ...... |
-- =============聚合函数===============
select count(StudentName) from student;-- 指定列(字段),忽略所有的null值
select count(*) from student; -- 不会忽略null值 本质计算行数
select count(1) from result; -- 不会忽略所有的null值 本质计算行数
select sum(StudentResult) as '总和成绩' from result;
select sum(StudentResult)/count(StudentNo) as '平均成绩' from result;
select AVG(StudentResult) as '平均成绩' from result;
select MAX(StudentResult) as '最高分' from result;
select min(StudentResult) as '最低分' from result;
数据库级别的MD5加密
MD5 : 主要增强算法和不可逆性
-- ======测试MD5=========
create table `TestMD5`(
`id` int(4) not null,
`name` varchar(20) not null,
`pwd` varchar(50) not null,
primary key(id)
)engine=innodb default charset=utf8;
-- 明文密码
insert into TestMD5 values(101,'Joey','123456'),(102,'Rose','125453'),(103,'Kobe','520025');
update TestMD5 set pwd=MD5(pwd);
-- 插入的时候加密
insert into TestMD5 values(104,'James',MD5('021351'));
-- 如何校验
select * form TestMD5 where `name`='James' and pwd=MD5('021351');