Mysql数据库 5.SQL语言聚合函数 语言日期-字符串函数

本文介绍了SQL中的聚合函数(如count,max,min,sum,avg),以及日期和字符串处理函数,包括时间添加、当前日期获取、拼接、大小写转换和substring用于字符串拆分。
摘要由CSDN通过智能技术生成

一、聚合函数

SQL中提供了一些可以对查询的记录的列进行计算的函数——聚合函数

1.count() 统计函数,统计满足条件的指定字符的值的个数

统计表中rebirth_mood个数

select count(列名) from 表名;

#统计表中rebirth_name='lcl'的个数

select count(列名) from 表名 where 限制条件;

2.max()函数,统计满足指定条件的字符最大的值 

查询表中rebirth_name最大的一项

select max(列名) from 表名;

查询加入条件判断的最大的一项

select max(列名) from 表名 where 约束条件 ;

 3.min()函数,统计满足指定条件的字符最小的值

 查询表中rebirth_name最小的一项

select min()列名 from 表名 where 约束条件;

4.sum()函数,计算和,查询满足条件的记录中,指定的列的总和

select sum(数据列) from 表名;

 查询表中某一列的总和

select sum(列名) from 表名 ;

查询表中某一列有了约束条件的项的总和

select sum(列名) from 表名 where 约束条件;

5.avg()函数,求平均值,查询满足条件的记录中 计算指定列的平均值

查询某一列中的平均值

select avg(列名) from 表名 ;

 

查询某一列中有了约束条件的项的平均值

select avg(列名) from 表名 where 约束条件 ;

二、日期函数和字符串函数

日期函数 

 时间添加和时间函数

#添加数据在rebir表中better_time的列
insert into rebirth(
	rebirth_name,rebirth_happen,rebirth_time,rebirth_mood,rebirth_go,better_time
)values(
	'lvcl','变好',20230929,'expected','insist','2023-10-26 21:43:00'
);

#时间赋值为当前时间 now()函数
insert into rebirth(
	rebirth_name,rebirth_happen,rebirth_time,rebirth_mood,rebirth_go,better_time
)values(
	'wp','变好',20230928,'health','health',now()
);

#观察新增数据
select * from rebirth;

now()函数得到当前日期

sysdate()函数获取当前日期 系统时间

#时间赋值为当前时间 now()函数
insert into rebirth(
	rebirth_name,rebirth_happen,rebirth_time,rebirth_mood,rebirth_go,better_time
)values(
	'lxr','变好',20231117,'health','better',sysdate()
);

修改表中数据 update

#修改名字为lxr的数据的happen项为merry
update rebirth set rebirth_happen ='merry' where rebirth_name='lxr';

三、字符串函数

就是通过SQL指令对字符串进行处理

拼接多列 contact函数

select contact(列名1,连接符号,列名2) from 表名;

#将name和go两字段拼接在一起 用中间的内容作拼接
select concat(rebirth_name,'-best-',rebirth_go) from rebirth ; 

uppper(列名)将该列字段的所有值转化为大写

lower(列名)将该字段的所有值转化为小写

upper(列名)将字段的值转为大写

select upper(列名) from 表名;

#upper(列名)将字段的值转为大写
select upper(rebirth_name) from rebirth ;

lower(列名)将该字段所有值转为小写

select  lower(列名) from 表名;

#lower (列名) 将所有字段的值转为小写
select lower(rebirth_name) from rebirth ;

substring函数 字符串拆分函数

substring(列名,截取开始位置,截取位数)

#substring(列名,截取开始位置,截取位数)截取字符串部分
select rebirth_name,substring(rebirth_time,5,4) from rebirth;

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值