数据库基本操作5——函数

一,基本:

1,概念:类似于java中的方法,将一组逻辑语句封装在方法体中,对外暴露方法名

2,好处:隐藏了函数实现的细节,提高了代码的重用性

3,调用:select 函数名(实参列表) 【from 表】

4,特点:叫什么(函数名) 干什么(函数功能)

5,分类:单行函数——如concat, length, ifnull

分组函数——做统计时使用,又称统计函数,聚合函数,组函数。

二,单行函数:

1,length

select length("josh");——返回4个字节,注意一个中文3个字节

select length("年后");——返回6个字节

2,concat

select concat("a","b","c");——abc

3,upper,lower

select upper("abc");——ABC

4,substr,substring

select substr('李莫愁爱上了 陆展元',7) as 结果;——陆展元,截取从指定索引后的字符

select substr('李莫愁爱上了 陆展元',1,3) as 结果;——李莫愁,截取指定索引的字符后,指定长度字符

5,inster

select inser('杨不悔爱上了殷六侠','殷六侠’) as out_put;——7,返回第一次出现指定字符发索引

6,trim

select trim(‘ 张翠山 ’);——张翠山,结果把前后的空格去了

select trim('a' from ‘aaaaaa张aaa翠山 aaaaaaaaa ’);——张aaa翠山,结果把前后的a去了

7,lpad rpad

select lpad("陈建江",10,'*');——*******陈建江,用指定字符填充,让所有字符数为10

8,replace

select replace('张无忌爱上了周芷若’,‘周芷若’,‘赵敏’);——张无忌爱上了赵敏,用指定字符去替换文本中的字符

二,数学函数

1,round 四舍五入

select round(1.56);——2

select round(-1.56);——-2

select round(1.567, 2);——1.57小数点后保留2位

2,ceil 向上取整

select ceil(1.001);——2

select ceil(-1.001);——-1

3,float 向下取整

select float(9.99);——9

select float(-9.99);——-10

4,truncate 截断

select truncate(1.69999,1)——1.6截断保留一位小数点后一位的数的其余数

5,mod 除余

mod(a,b)——a-a/b*b

select mod(10,3);——3

三,日期函数

1,now 返回当前系统的日期

select now();

2,curdata 返回当前系统的日期,不包含时间

select curdata();

3,curtime 返回当前系统的时间,不包含日期

select curtime();

4,指定获得指定的部分,年,月,日,时,分,秒

select year(now());

5,str_to_date 将字符串转化成日期类型

select str_to_date('1998-04-03','%Y-%c-%d');——1998-4-3

4,date_to_format 将时间格式转化成字符

select str_to_date('1998-04-03','%Y年-%c月-%d号');——1998年4月3号

三,其他函数:

version()

datebase()

user()

四,流程控制函数

if

if else

-----------------------------------------

case 要判断的字段或者表达式

when 常量1 then 要显示的值1或者语句1;

when 常量2 then 要显示的值2或者语句2;

。。。

else 要显示的值n或者语句n;

end

实例:select if(10<5,'大',‘小’);前面式子成立就返回第一个参数,否则返回第二个

select name case department_id

when 30 salary*1.1

when 40 salary*1.2

when 50 salary*1.3

else salary

end as 新工资

from employee

----------------------------------------

case

when 条件1 then 要显示的值1

when 条件2 then 要显示的值2

when 条件3 then 要显示的值3

。。。

else 要显示的值n

实例:

select salary

case

when salary>20000 then 'A'

when salary>15000 then 'B'

when salary>10000 then 'C'

else 'D'

end as 工资级别

from employee;

三,分组函数:

1,简单的使用

select sum(salary) from employee——求和,将所有工资字段求和输出

select avg(salary) from employee——平均值

select min(salary) from employee——最小值

select max(salary) from employee——最大值

select countsalary) from employee——非空工资字段格式

select sum(salary) ,avg(salary), min(salary) from employee

2,参数支持哪些类型

select sum(name);——x, sum, avg只支持数值型

select max(name);——ok,max, min, count可以支持字符型,因为英文字符可以排序

3,忽略null值

select sum(bonus) from employee;——bonus的列表中有null值,忽略null值

4,可以和distinct搭配使用

select sum(distinct salary) from employee;——去重后求和

5,count函数的详细介绍

select count(*) from employee;——用来统计行数

select count(1) from employee;——常量值可以用来统计行数

6,和分组函数一同查询的字段是group by 后的字段

7,datediff()

select datediff(now(),'1998-04-03') as 活了多少天;

猜你喜欢

转载自blog.csdn.net/weixin_44841312/article/details/105856019
今日推荐