zoukankan      html  css  js  c++  java
  • 分组查询 group by having 排序 order by asc(升序)或desc(降序)

    1.查询排序:order by asc(升序)或desc(降序),默认asc(升序)

    查询选修了 3号课程的学生的学号及其成绩,查询结果按分数的降序排列。

    select sno,score from sc where cno='c3' order by score desc

    查询全体学生情况,查询结果按所在系升序排列。

    select * from s order by dept  asc

    查询全体学生情况,对同一系中的学生按年龄降序排序。

    select * from s order by 4 desc --"4"表示"select *"查询列中的第四列

    查询全体学生情况,查询结果按所在系升序排列,对同一系中的学生按年龄降序排序.

    select * from s order by dept,4 desc --"4"表示"select *"查询列中的第四列.默认asc(升序)


    2.分组查询 group by:

          将查询结果表的各行按一列或多列取值相等的原则进行分组。

          例:查询各个课程号与相应的选课人数。

           select  cno,count(sno) from sc group by cno


          分组后还要求按一定的条件对这些组进行筛选,最终只输出满足指定条件组的统计值,可以使用having 子句

          例:查询有五人以上学生选修的课程号及选修人数。

          select cno,count(sno) from sc group by cno having count(*)>=2

        数据库表s,c,sc截图请到http://www.cnblogs.com/fuge/archive/2012/03/16/2400913.html

  • 相关阅读:
    [Next] 六.next的优化
    [Next] 五.next自定义内容
    Mac解决端口占用
    [Next] 四.在next中引入redux
    [Next] 服务端渲染知识补充
    [Next] 三.next自定义服务器和路由
    哪些使用UDP、TCP、IP协议
    IDEA配置git
    ssm整合配置
    git传输远程仓库
  • 原文地址:https://www.cnblogs.com/fuge/p/2342571.html
Copyright © 2011-2022 走看看