MySQL 常用内置函数 数学函数 字符串函数 日期函数 判断函数

MySQL 常用内置函数

  • 数学函数
  • 字符串函数
  • 日期函数
  • 条件函数
  • 加密函数

数学函数

  • abs(n): 求一个数字的绝对值
  • sqrt(n): 求一个数字的 正的算术平方根
  • mod(n, m) : 求 n/m 的 余数
  • ceil(n) : 对一个数字 向上 取整 3.1 ----> 4
  • floor(n) : 向下取整
  • round(n) : 四舍五入取整
  • round(n, p) : 四舍五入 保留 p 位小数
  • rand() : 返回一个 0-1 的随机数,包含 01
  • truncate(n, p) : 保留 p 位小数
  • pow(x, y) : xy次幂

字符串函数

  • char_length(s) : 获取字符串的长度
  • length(s) : 获取字符串所占用的字节数
  • lower(s) / lcase(s) : 将字符串转小写
  • upper(s) / ucase(s) : 将字符串转大写
  • left(s, len) / right(s, len) : 左/右截取字符串 指定长度
  • concat(s, s1, ...) : 拼接字符串
  • concat_ws(sep, s, s1, ...) : 以 sep 作为分隔符,进行字符串的拼接
  • trim(s) , ltrim(s) , rtrim(s) : 删除空格/左空格/右空格
  • substr(s, start, len) : 从 start 开始截取,截取 len长度 返回子串,start 从1开始
  • substring(s, start, len): 和 substr用法相同

日期函数

  • now(): 用来获取当前系统时间
  • month(date) : 获取对应日期的月份
  • year(date) : 获取对应日期的年份
  • dayofweek(date) : 返回星期, 星期日 返回 1,…
  • dayofyear(date) : 获取今天是一年中的第几天
  • datediff(date1, date2) : 返回两个日期之间相差的天数
  • date_format(date, format) : 对日期进行格式化处理
  • adddate(date, interval 1 year) : 增加一年
  • adddate(date, interval 1 month) : 增加一月
  • adddate(date, interval 1 day) : 增加一天
  • subdate: 具体用法和 adddate 相同,代表 减少
format:  
	%Y : 代表年份
	%m : 代表月份
	%d : 代表 天
	%H : 代表 24进制的小时
	%i : 代表 分
	%s : 代表 秒

判断函数

  • if(expr, v1, v2) : expr 表达式为 true, 返回 v1, 否则 返回 v2
-- 查询 姓名、成绩、及是否及格

select name, score , if(score >=60 ,'及格', '不及格') from t_score ;

  • ifnull(n, default): 如果 nnull, 则返回 default ,否则返回 n
-- 查询所有成绩、没有考试的以 0分 处理
select name , ifnull(score, 0) from t_score ;
  • case 函数
实现多分支条件判断

case  expr  when v1  then ..  when v2 .. then ..... esle .. end ;


case when expr then ... when expr then ...... else .... end ;

select name , score , 
case 
	when score >=90 then '良'
  when score >=80 then '优'
	when score >=70 then '中等'
	when score >=60 then '及格'
	else '不及格'
end 
from t_score ;

case 实现行列转换

select name 姓名  ,
max(case subject when '语文' then score  end) 语文, 
max(case subject when '数学' then score  end) 数学,
max(case subject when '英语' then score  end) 英语
from t_score  group by name ;

加密函数

  • md5(str) : 一般用来实现密码的加密,不可逆的,无法进行解密
  • password(str) : MySQL数据库 账号的密码采用的 是 password 加密

MySQL数据库给账户设置密码

  • 在登录状态下、修改密码
set password = password('新密码');

MySQL 内置的用户 和密码, 在 mysql.user 表存储的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值