MySQL的使用(5)-函数查询

#############字符串函数#########
(1) concat()的使用
select concat("name:",ename,'部门:',deptno) from emp where deptno=10;
(2) length()的使用
select * from emp where length(ename)=6;
(3) lower() upper() 全部设置为大写小写
select lower(ename)from emp where deptno=10;
select upper(ename)from emp where deptno=10;
(4)replace(str, oldstr, newstr)
select ename,job, replace(job,'clerk',"staff") as newjob from emp where job="clerk"
(5)substring(str, index, n)) 获得字符串的子字符串
select ename,job, substring(ename,2,3) as newname from emp where job="clerk"
###############数值函数#################
1  abs() ceil() floor() mod() pi() pow() rand() round() truncate()
select abs(-3); # 绝对值
select ceil(2.7); # 向上取整
select floor(2.7); # 向下取整
select mod(3,2); # 取余
select pi(); #  Π
select pow(2,3); # 求一个数的N次方
select rand(),rand(),rand(); #随机产生
# 保留n位小数进行四舍五入保留
select round(3.323),round(3.223,1),round(3.236,2) 
# 不进行四舍五入保留直接截断,必须写入保留几位不能默认
select truncate(3.323,0),truncate(3.223,1),truncate(3.236,2)
#################日期与时间函数#################
1 curdate()返回当前年月日
  curtime()返回当前时分秒
	now()返回年月日时分秒
select curdate(),curtime(),now();
2 sysdate() 返回该函数执行的年月日时分秒,与now区别在于其返回的在sql语句开始执行的年月日时分秒
select sysdate(),now(),sleep(2),sysdate(),now();
3 dayofyear() week()  分别获取某个日期所在年份的第几天,第几周
select dayofyear("2020-2-24"),week("2020-2-24");
4 date_format(date,format)
select date_format(now(),"%Y-%m-%d %H:%i:%s")
5 date_add(date,interval) date_sub(date,interval) 实现日期的加减运算
select date_add(now(),interval 1 hour),now();
6 datediff() 计算两个日期的时间差
select datediff("2021-10-1",now());
####################流程函数################
1 使用if()函数查询
select sal,ename,if(sal>=3000,"hign","low")sal_level from emp where deptno=20
2 ifnull()
select ename,deptno,job,sal*12+comm as year_sal from emp where deptno=30
select ename,deptno,job,sal*12+ifnull(comm,0) as year_sal from emp where deptno=30
3 nullif(value1,value2) 相等返回null 不等返回value1
select nullif(1,2),nullif(1,1);
4 case when then () 函数使用 
select ename,deptno,sal,case sal>=3000 when true then "high" else "low" end sal_level from emp where deptno=20
#############多行函数########################
1 count()函数使用
select count(*),count(comm),count(distinct(mgr)) from emp;
select count(distinct(ifnull(mgr,0))) from emp
2 sum() avg() distinct() 删除重复
select sum(sal),avg(sal),sum(distinct(sal)),avg(distinct(sal)) from emp;
3 max() min()
select max(sal),min(sal) from emp;

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值