zoukankan      html  css  js  c++  java
  • 五、Oracle 分组查询、视图

    一。分组函数
    1.avg:平均分
    2.sum:求和
    3.max:最大值
    4.min:最小值
    注意:前面四个必须针对数值字段,且参数只能是一个
    5.count:求个数

    二。分组查询
    1。语法是 group by 分组字段1,分组字段2...
    2.分组后可以使用分组函数。
    3.分组函数不能和其他字段一起显示,除了分组字段。
    4.分组查询之后还要做条件限制的话,用having子句
    5.关键字的顺序:select ...from...where...group by...having...order by....

    三。子查询:在查询的结果上继续查询

    1.将查询结果作为新的表继续查询
    select * from student a,(select * from grade) b where a.xxid=b.xxid
    2.将查询结果作为查询条件来使用
    select * from grade where student in (select st_id from student where st_sex='女')

    四。视图(view)
    1.作用。保存查询结果,方便反复调用
    2.语法:create view 视图名 as 查询语句
    3.注意:视图的数据可以修改,但不建议修改。统计后的数据,用了函数的数据,以及非键保留值得数据都不能修改

     练习:

     1.查询每门课程的最高分和最低分,显示如下
      java最高分:99  最低分:33
      oracle最高分:100  最低分: 55
    select a.dic_name,max(b.socre) 最高分,min(b.socre) 最低分
    ,avg(b.socre) 平均分,sum(b.socre) 总和 from diclipline a,grade b 
    where a.dic_id=b.diclipline
    group by a.dic_name
    
    2.查询每门成绩的平均分和总分
    
    3.查询java1班参加oracle考试的人数
    select count(rownum) from squad a3,diclipline b3,grade c3,student d3 where a3.squad_id=d3.st_class 
    and b3.dic_id=c3.diclipline and c3.student=d3.st_id 
    and a3.squad_name='java1班' and b3.dic_name='oracle'
    
    4.查询每个班j2ee考试的平均分,并按降序排序
    select a4.squad_name,avg(c4.socre) from squad a4,diclipline b4,grade c4,student d4 where a4.squad_id=d4.st_class 
    and b4.dic_id=c4.diclipline and c4.student=d4.st_id and b4.dic_name='j2ee'
    group by a4.squad_name order by avg(c4.socre) desc
    
    5.查询oracle考试平均成绩不及格的班级有哪些
    select avg(c5.socre),a5.squad_name,b5.dic_name from squad a5,diclipline b5,grade c5,student d5 where a5.squad_id=d5.st_class 
    and b5.dic_id=c5.diclipline and c5.student=d5.st_id and b5.dic_name='oracle'
    group by a5.squad_name,b5.dic_name having avg(c5.socre)>60
    
    6.查询所有java考试不及格的学员是哪些
    select * from grade where socre<60
  • 相关阅读:
    在.net中使用redis(StackExchange.Redis)
    基于windows平台的命令行软件安装工具Chocolatey的安装
    c#的日志插件NLog基本使用
    微信小程序的组件总结
    markdown语法简介
    微信小程序基础语法总结
    leetcode-mid-sorting and searching
    leetcode-mid-sorting and searching
    leetcode-mid-sorting and searching -347. Top K Frequent Elements
    leetcode-mid-sorting and searching-34 Search for a Range
  • 原文地址:https://www.cnblogs.com/wlxslsb/p/10711556.html
Copyright © 2011-2022 走看看