SQl Server 函数篇 聚合函数

说一下数据库中的聚合函数

函数使用必须加小括号(),

5种聚合函数:

1、max最大值   select max(price) from car where code='c024'   --取这一列中的最大值,但是显示出的数值不属于任何列,只是一个值,但也可用作比较

2、min最小值   select * from car where oil= (select min(price) from car)    --取这一列中的最小值

3、avg平均值    select avg(price) as 平均价格  from car   -- 标黄的就是 其别名,因为他不属于任何列,所以可以让他显示的时候定义一个名字,也可以不用as 用空格+汉字

4、sum求和    select sum (price) from car    

5、count数量  select count (*) from car      -- 有多少行

起别名:

  select avg(price) as 平均价格  from car     查询出来的列名就会变成  as 之后的名字,也可以不用as  直接空格 后面接别名

  select avg(price)  平均价格  from car   不行的话就把别名用  ' '   夹起来

  也可以用作查询  selcet *from student t   where t.id>0   别名 t 相当于  student 对象

1.select * from car oil in (7.4,8.5,8.6)        --oil是列名

            --in 在括号里面的值都会走一遍,也就是说括号内就是条件,oil in 就是 oil 有(值)中的值

2.select * from car oil not in (7.4,8.5,8.6)

            -- 就是不在里面,与上面说的相反

3.select *from car where oil >=7 and oil <=8

           --  and 就是并且

4.select * from car where oil between 7 and 8

          --   between    and     就是再两者之间,这里是 大于等于7且小于等于8

5.select *from car where oil >any(select oil from car where code='c024' or code ='c014')

          --  any  ()  大于最小值,小于最大值( 这里就是大于括号内的任何一个值就成立,要表达的意思就是大于最小值)

6.select * from car where oil >all(select oil from car where code='c024' or code ='c014')

          --  all ()    大于最大值,小于最小值(这里表达要大于括号内的所有值

7. select  distinct oil  from car

     --   distinct  去重,针对某一列,不能用 *  

原文地址:https://www.cnblogs.com/big-lll/p/6542054.html