官网:https://dev.mysql.com/doc/refman/5.7/en/func-op-summar-ref.html
5.1、常用函数
SELECT ABS(-8)
SELECT CEILING(9.4)
SELECT FLOOR(9.4)
SELECT RAND()
SELECT SIGN(-10)
SELECT CHAR_LENGTH('即使再小的帆,也能远航')
SELECT CONCAT('我','爱','你们')
SELECT INSERT('我爱编程helloworld',1,2,'超级热爱')
SELECT LOWER('KKKKKAAA')
SELECT UPPER('afsjdf')
SELECT INSTR('asdfghjk','h')
SELECT REPLACE('坚持就能成功','坚持','努力')
SELECT SUBSTR('坚持就能成功',4,6)
SELECT REVERSE('坚持就能成功')
SELECT REPLACE(studentname,'张','章')
FROM student
WHERE studentname LIKE '张%'
SELECT CURRENT_DATE()
SELECT CURDATE()
SELECT NOW()
SELECT LOCALTIME()
SELECT SYSDATE()
SELECT YEAR(NOW())
SELECT MONTH(NOW())
SELECT DATE(NOW())
SELECT HOUR(NOW())
SELECT MINUTE(NOW())
SELECT SECOND(NOW())
SELECT SYSTEM_USER()
SELECT USER()
SELECT VERSION()
5.2、聚合函数(常用)
函数名称 | 描述 |
---|
COUNT() | 计数 | SUM() | 求和 | AVG() | 平均值 | MAX() | 最大值 | MIN() | 最小值 | … | … |
SELECT COUNT(studentname) FROM student;
SELECT COUNT(*) FROM student;
SELECT COUNT(1) FROM student;
SELECT SUM(studentresult) AS 总和 FROM result
SELECT AVG(studentresult) AS 平均分 FROM result
SELECT MAX(studentresult) AS 最高分 FROM result
SELECT MIN(studentresult) AS 最低分 FROM result
5.3、数据库级别的MD5加密(扩展)
什么是MD5?
主要增强算法复杂度和不可逆性。
MD5 不可逆,具体的值的 md5 是一样的
https://www.cmd5.com/
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,'lisi','123456'),(3,'wangwu','123456')
UPDATE testmd5 SET pwd=MD5(pwd) WHERE id=1UPDATE testmd5 SET pwd=MD5(pwd)
INSERT INTO testmd5 VALUES(4,'小明',MD5('123456'))
SELECT * FROM testmd5 WHERE `name`='小明' AND pwd=MD5('123456')
|