zoukankan      html  css  js  c++  java
  • 连接查询和分组查询

    连接查询和分组查询

    1.在SQL Server语言中叫做分组查询,分组查询采用Group  BY 子句实现。

    二,多列分组查询

    2.分组查询还可以按照多个列来进行分组。

    使用Group   BY 关键字时,是有限制的,在SELECT 列表中可以指定列是有限制的,仅允许以下几项。

    被分组的列为每个分组返回一个值,

    --查询年级所拥有的人数 select GradeId as 年级,COUNT(Phone) as 人数 from Student

    group by GradeId 

    --根据性别进行分组 select Sex as 性别,COUNT(*) as 人数

    from Student group by Sex

    --查询每门课程的平均分 select SubjectId as 课程编号,AVG(StudentResult) as 平均分

    from Result group by SubjectId

    --按地区分类,查询地区的人数 select COUNT(*) as 人数,Address as 地址

    from Student group by Address

    --查询每门课程的平均分,并且按照分数由低到高的顺序排列显示 select SubjectId as 课程编号,AVG(StudentResult) as 平均分

    from Result group by SubjectId order by AVG(StudentResult) desc

    --统计每学期男女同学的人数 select COUNT(*) as 人数,GradeId as 年级,Sex as 性别

    from Student Group by GradeId,Sex order by GradeId --性别:男和女 年级:1,2,3

    --如何获得总人数超过2人的年级 select COUNT(*) as 人数,GradeId as 年级,Sex as 性别

    from Student Group by GradeId,Sex having COUNT(*)>=2 order by GradeId

    --出生日期大于1990年的学生,获得总人数超过2人的年级 select COUNT(*) as 人数,GradeId as 年级,Sex as 性别

    from Student where BornDate >'1990/01/01' Group by GradeId,Sex having COUNT(*)>=2 order by GradeId

    --同时从这两个表中取得数据 select Student.StudentName as 姓名,Result.StudentResult as 成绩, Result.SubjectId AS 科目编号

    from Student,Result where Student.StudentNo=Result.StudentNo

    表连接
    select 列 from 表1,表2
    where 条件(表1.主键列=表2.外键列)

    内连接(inner join)
    select 列 from 表1 inner join 表2
    on 条件(表1.主键列=表2.外键列)

    as也可以为表赋别名

    select S.StudentName as 姓名,R.StudentResult as 成绩, R.SubjectId AS 科目编号

    from Result as R inner join Student as S on(S.StudentNo=R.StudentNo)

    slect S.StudentName as 姓名,R.StudentResult as 成绩, SU.SubjectName AS 科目名称

    from Result as R inner join Student as S on(S.StudentNo=R.StudentNo) inner join Subject as SU on(R.SubjectId=SU.SubjectId) 

    select Student.StudentName as 姓名,Result.StudentResult as 成绩, Subject.SubjectName AS 科目名称

    from Student,Result,Subject where Student.StudentNo=Result.StudentNo and Result.SubjectId=Subject.SubjectId

    --左外连接

         左外连接(left join)
    select 列
    from 主表
    left outer join 从表
    on (表1.主键列=表2.外键列)

    select S.StudentName,R.SubjectId,R.StudentResult

    From Student AS S LEFT JOIN Result as R on(S.StudentNo=R.StudentNo)

    --右外连接

         右外连接(right join)
    select 列
    from 从表
    right outer join 主表
    on (表1.主键列=表2.外键列)

    select S.StudentName,R.SubjectId,R.StudentResult

    From Result AS R RIGHT JOIN Student as S on(S.StudentNo=R.StudentNo)

  • 相关阅读:
    Hdu-5983 2016ACM/ICPC亚洲区青岛站 B.Pocket Cube 模拟
    Codeforces Round #300 F
    cf298F:状压dp+剪枝
    POJ3294--Life Forms 后缀数组+二分答案 大于k个字符串的最长公共子串
    jzp线性筛及其简单应用
    Codeforces Round #299 (Div. 1)C. Tavas and Pashmaks (凸壳)
    Codeforces Round #236 (Div. 2)E. Strictly Positive Matrix(402E)
    【2012天津区域赛】部分题解 hdu4431—4441
    HDU4436---str2int 后缀树组(12年天津区域赛)
    Codeforces
  • 原文地址:https://www.cnblogs.com/luoxionghenku/p/7743322.html
Copyright © 2011-2022 走看看