数据库 聚合 、分组、筛选、排序、日期等函数的应用

版权声明:版权归剑云工作室所有 https://blog.csdn.net/qq_40456829/article/details/82533279

聚合函数:

       对一组值计算。

聚合常用函数
函数 介绍 实例
count(*) )   
  
统计总行数 (不忽略空值) select count(*) from table
max(列) 求该列的最大值 select SNa,SN from S where Age in (select max(Age) from S 
min(列 ) 求该列的最小值 select min(id) from table
sum(列) 求列的和 select sum(Age) from table   
 avg(列) 求列的平均值   select avg(Age) from table

分组 group by:

        按照%%归类 分析查询

        实例:SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer


筛选 where  VS   having:

     having  先分类-分类后筛选        实例:  select sex,count(*) as Age from STUDENT group by sex having Age > 2
     where 是对行的 第一次筛选,对原始数据的筛选
     having 是二次筛选,是对group by分组后的结果进行筛选


排序  order  by:

                --asc升序排列                         select * from S order by Age asc
                --desc降序排列                        select * from S order by Age desc 


日期比较:
            当前日期函数:getdate()          select* from SC where Rdate < GETDATE()


union 操作符 :

           合并两个或多个 SELECT 语句的结果集   【相同数量列】 【相似数据类型】 【列顺序必须相同】
            实例:select SN from S where Age >19  union all  select SN  from S where Dept = '计算机' 

猜你喜欢

转载自blog.csdn.net/qq_40456829/article/details/82533279