官网:https://dev.mysql.com/doc/refman/5.7/en/func-op-summary-ref.html

常用函数

-- ======================== 常用函数 ========================
-- 数学运算
select ABS(-9);
select ceiling(5.4); -- 向上取整
select floor(5.6);  -- 向下取整
select RAND();  -- 返回一个 0~1 之间的随机数
select SIGN(9);  -- 判断一个数的符号:正数返回 1
select SIGN(-2);  -- 判断一个数的符号:正数返回 -1

-- 字符串函数
-- 字符串长度
select CHAR_LENGTH("北京欢迎你");
-- 拼接字符串
select CONCAT("即使再小","的帆","也能远航");
-- 查询并替换,从某个位置开始替换某个长度
-- INSERT(str1,str1的起始点,要被替换的长度【从起始点算起】,str2);
select INSERT("我爱编程",2,1,"超级热爱");  
-- 全部转为大写
select UPPER("ASdas");
-- 全部转为小写
select LOWER("DXSsda");
-- 返回第一次出现子串的索引
select INSTR("beijing hbsb","b");
-- 替换指定字符串
select REPLACE("狂神说坚持就能成功","坚持","努力");
-- 返回指定的子串
-- 参数二:开始截取的位置 参数三:截取的长度
select SUBSTR("狂神说坚持就能成功",2,4);
-- 字符串反转
select REVERSE("北京");


-- 查询姓 周 的同学,将周替换为 邹
select `StudentName` from `student` where `StudentName` like "周%"; 

select replace(`StudentName`,'周','邹') 
from `student` 
where `StudentName` like "周%"; 


-- 时间和日期函数(记住)
select CURRENT_DATE();  -- 获取当前日期:2020-03-14
select CURDATE();  -- 获取当前日期:2020-03-14
select NOW();   -- 获取当前的时间
select localtime();  -- 获取本地时间
select sysdate();  -- 获取系统时间


-- 系统
select SYSTEM_USER(); -- root@localhost
select USER();  -- root@localhost
select version(); -- 5.7.29

聚合函数(常用)

函数名称 描述
COUNT() 计数
SUM() 求和
AVG() 平均值
MAX() 最大值
MIN() 最小值
-- ============================ 聚合函数 =============================
-- 都能统计表中的数据(想查询一个表中有多少记录,就用count())
select count(`StudentNo`) from `student`;  -- count(字段)   会忽略所有null值
select count(*) from `student`;   -- count(*)  不会忽略null值        本质:计算行数
select count(1) from `student`;   -- count(1)  不会忽略所有的null值  本质:计算行数

select SUM(`StudentResult`) as 总和 from result;
select AVG(`StudentResult`) as 平均值 from result;
select MAX(`StudentResult`) as 最大值 from result;
select MIN(`StudentResult`) as 最小值 from result;

数据库级别的MD5加密(扩展)

什么是MD5?

主要增强算法复杂度和不可逆性。

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(1,'zhangsan','123456'),(2,'1isi','123456'),(3,'wangwu','123456');


-- 加密
update testmd5 set pwd=MD5(pwd) where id=1;

update testmd5 set pwd=MD5(pwd)

-- 插入的时候加密
insert into testmd5(id,`name`,`pwd`) values (4,'xiaoming',MD5("dasd"));

-- 如何校验:将用户传递进来的密码,进行MD5加密,然后比对加密后的值
select * from `testmd5` where `name`='xiaoming'  and `pwd`=MD5("dasd");