-
官网:https://dev.mysql.com/doc/refman/5.7/en/sql-function-reference.html
-
常用函数
- 数学运算函数
- 绝对值:select abs()
- 向上取整:select ceiling()
- 向下取整:select floor()
- 返回一个0-1的随机数:select rand()
- 判断一个数的符号:select sign()
- 字符串函数
- 字符串长度:select char_length()
- 拼接字符串:select concat()
- 查询替换:select insert()
- 转大写:select upper()
- 转小写:select lower()
- 第一次出现的索引:select instr()
- 替换出现的指定字符串:select replace()
- 截取字符串:select substr()
- 反转:select reverse()
- 时间和日期函数
- 获取当前日期:select current_date()
- 获取当前日期:select curdate()
- 获取当前时间:select now()
- 获取本地时间:select localtime()
- 获取系统时间:select sysdate()
- 系统函数
- 当前用户:select system_user()
- 当前用户:select user()
- 版本:select version()
- 数学运算函数
-
聚合函数(常用)
函数名称 描述 count() 计数 sum() 求和 avg() 平均值 max() 最大值 min() 最小值 -
以下三种方法都能统计表中的数据
select count(`studentName`) from `student`; select count(*) from `student`; select count(1) from `student`;
- 区别:
- count(字段):会忽略该字段中所有的null值
- count(*):不会忽略该字段中所有的null值,本质:计算行数
- count(1):不会忽略该字段中所有的null值,本质:计算行数
- 区别:
-
函数用法
select sum(`score`) as 总和 from result select avg(`score`) as 平均分 from result select max(`score`) as 最高分 from result select min(`score`) as 最低分 from result
-
-
数据库级别的MD5加密(加密后不可逆,但值不变)
测试MD5加密
update 表名 set pwd=MD5(pwd) where id = 1; -- 插入的时候加密 insert into 表名 values(4,'小明',MD5('123456'));
校验:将用户传递进来的密码进行md5加密,然后在对加密后的值进行比对
select * from 表名 where `name`='小明' and pwd=md5('123456')
MySQL函数
最新推荐文章于 2023-07-27 13:48:04 发布