mysql整理6-函数分类整理

MySQL中函数大体分类:

单行函数
	数学函数
		select PI()* 2 *2;  #pi
		CEIL(-12.3); #向上取整
		FLOOR(12.3); #向下取整
		ROUND(8.45,-1); #四舍五入
		MOD(5,2);  #取模
		RAND();  #随机数 [0,1)
		POW(2,3); #幂运算
	字符函数
		LENGTH('this is a dog');   #获取长度
		     length(ename) from emp;
		LOWER('THIS');
		UPPER('this');
		SUBSTR('this is zs',1,6); #下标从1开始
		REPLACE(str,from_str,to_str);
		trim(' this is '); #去两端空格
		LPAD('aa',10,'*');  #左填充
		RPAD('aa',10,'*');  #右填充
	日期函数
		NOW();  #当前时间 (data+time)
		SYSDATE();  #获取系统时间
		CURRENT_DATE()   CURDATE()
		CURRENT_TIME()  CURTIME()
		YEAR MONTH DAY
		#获取当前月最后一天
		select LAST_DAY('2018-02-02');
		#日期计算
		select DATE_ADD(NOW(),interval 2 MONTH);
聚合函数
	max min avg count sum
分组函数
	group
	分组聚合一般联合使用
加密函数
	MD5('') SHA() password()

1)单行函数

数学函数

 select PI()* 2 *2;  #pi
 select CEIL(-12.3); #向上取整
 select FLOOR(12.3); #向下取整
 select ROUND(8.45,-1); #四舍五入
 select MOD(5,2);  #取模
 select RAND();  #随机数 [0,1)
 select POW(2,3); #幂运算 
 #随机从emp中获取两条记录。
 select * from emp order by RAND() limit 2;

字符函数

 select LENGTH('this is a dog');   #获取长度
 select length(ename) from emp; 
 select LOWER('THIS'); 
 select UPPER('this');
 select SUBSTR('this is zs',1,6); #下标从1开始
 #select REPLACE(str,from_str,to_str);
 select trim(' this is '); #去两端空格
 select LPAD('aa',10,'*');  #左填充
 select RPAD('aa',10,'*');  #右填充

日期函数

	select NOW();  #当前时间
	select SYSDATE();  #获取系统时间
	select CURRENT_DATE();
    select CURDATE();
	select CURRENT_TIME();
    select CURTIME();
    select YEAR('1998-09-09');
	select YEAR(NOW());
	select MONTH(date);
	select DAY(date);
	#获取当前月最后一天
	select LAST_DAY('2018-02-02');
	#日期计算
	select DATE_ADD(NOW(),interval 2 MONTH);

2)聚合函数

min() max() avg() count() sum()

	select max(sal) from emp;
	select min(sal) from emp;
	select avg(sal) from emp;  
	select count(*) from emp;  #记录数
	select count(1) from emp;  #记录数
	select count(comm) from emp; #字段非空总数
	select sum(sal) from emp;

3)分组函数

分组 group by 分组条件 having:分组之后进行检索

  select deptno,avg(sal) from emp group by deptno;
	
 # 查询平均工资大于2000的部门的编号和平均工资。
  		 # 1.where在group by之后
  		 # 2.where中不能使用聚合函数
   select deptno,avg(sal) from emp group by deptno having avg(sal) > 2000;

4)加密函数

 select MD5('root');
 select SHA('root');
 select password('root');
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值