暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SQLServer的基础查询

全栈精英 2021-08-02
530

单表查询 

    -- 查询2016年~2017年入学的记录
    select * from student
    where s_regDate
    between '2016-1-1'
    and '2017-12-31'

    select * from student
    where s_regDate >= '2016-1-1'
    and s_regDate <= '2017-12-31'

    select s_id as 学号, s_regDate 入学日期, year(s_regDate) as 年份 , MONTH(s_regDate) as 月份 from student
    where year(s_regDate)
    between 2016
    and 2017

    -- 查询姓张的学生记录
    select * from student where s_name like '张%' -- % :任意数量的任意字符
    select * from student where s_name like '张_' -- _ : 一个数量的任意字符

    -- 排序的查询输出
    select * from student order by s_id desc -- desc :倒序, asc :正序(默认)
    select * from student order by s_name desc

    select s_id, s_class from student
    order by s_class ,s_id desc -- 多值排序的规则:先按班级排序,如果班级有相同,则按学号排序
    复制

    复制

    多表查询 

      -- 查询有成绩的学生信息和分数
      -- 写法1:
      select a.s_id 学号, s_name 姓名, c_id 课程ID, sc_score 分数
      from student a , score b -- 需要查询的多张表,用逗号分开,可以为每张表分配一个别名
      where a.s_id = b.s_id -- 连接关系:主表.主键 = 从表.外键

      -- 写法2:
      select a.s_id 学号, s_name 姓名, c_id 课程ID, sc_score 分数
      from student a join score b -- join : 表示 inner join 内链接
      on a.s_id = b.s_id -- on :建立表连接关系


      -- 外连接:查询所有的学生信息和分数
      select a.s_id 学号, s_name 姓名, c_id 课程ID, sc_score 分数
      from student a left join score b -- left join : 表示 左外连接
      on a.s_id = b.s_id -- on :建立表连接关系

      select a.s_id 学号, s_name 姓名, c_id 课程ID, sc_score 分数
      from student a right join score b -- right join : 表示 右外连接
      on a.s_id = b.s_id
      复制
        
      复制

      外连接查询结果的NULL处理

      ISNULL是SQLServer的内置函数,表示如果当前的字段是NULL,则用默认值代替输出 

        -- 外连接查询如何处理查询结果的NULL值
        select a.s_id 学号, s_name 姓名,
        ISNULL(convert(varchar(10),c_id),'无') 课程ID,
        ISNULL(convert(varchar(10),sc_score) ,'未考') 分数 -- 每一列的数据类型要相同,所以类型转换
        from student a left join score b -- 左外连接
        on a.s_id = b.s_id
        复制

        复制

        查询里面的条件判断 case ... when 语句

        格式: 

          case when 字段的判断条件1  then  输出结果1
          when 字段的判断条件2 then 输出结果2
          else 默认结果
          end
          复制

          复制

          案例:输出成绩的评定结果

             select s_id 学号, c_id 课程ID, 
            case when sc_score<60 then '不及格' -- if (分数低于60) 输出“不及格”
            else convert(varchar(10), sc_score) -- else 输出分数
            end 分数
            from score

            -- 根据成绩输出对应的级别
            select *, -- 输出原有的score的字段
            case when sc_score>=90 then '优' -- if (分数>=90)
            when sc_score>=80 then '良' -- else if (分数>=80)
            when sc_score>=70 then '中' -- else if (分数>=70)
            when sc_score>=60 then '及格' -- else if (分数>=60)
            else '不及格' -- esle 默认值
            end 分数级别 -- 动态创建的列
            from score
            复制

            复制

            统计查询

             
            复制
              -- 统计总共有多少个班级
              select count(distinct s_class) 班级数量 from student -- distinct去掉查询结果的重复项

              -- 统计学号ST001的平均分
              select AVG(sc_score) 平均分 from score where s_id='ST001'

              -- 统计每个人平均分
              select AVG(sc_score) 平均分 , s_id 学号 -- 包含非统计字段的查询都必须使用分组
              from score
              group by s_id -- 分组统计

              -- 统计各班男女生人数
              select count(s_id) 人数, s_class 班级, s_sex 性别 -- 在select部分包含两个非统计字段班级和性别
              from student -- 所以这个两个字段必须要参与分组
              group by s_class, s_sex -- 按班级和性别分组
              order by s_class -- 排序班级再输出

              -- 统计没有参加考试的人数
              select count(*)
              from student left join score -- 查询没有参加考试的人(外连接)
              on student.s_id = score.s_id -- 连接关系
              where sc_score is null -- 分数为null的是未考
              复制

              复制

              子查询

              子查询的三种格式:

              • 子查询位于条件部分:select * from 表 where 字段 in (子查询)

              • 子查询位于表部分:   select * from 表, (子查询) t where 表.关系字段 = t.关系字段

              • 子查询位于select部分:select *, (子查询) from 表 

                -- 子查询最高分的学生记录
                select * from student where s_id in
                (select s_id from score where sc_score = (select max(sc_score) from score))

                -- 统计每门课的最高分的学号
                select s_id
                from score,
                (select max(sc_score) as m , c_id
                from score
                group by c_id) t
                where score.c_id = t.c_id -- 连接关系1:分数表.课程ID = t.课程ID
                and score.sc_score = t.m -- 连接关系2:分数表.分数 = t.最高分
                复制

                复制

                联合查询:union

                   -- 输出成绩、小计平均分、总分
                  select a.s_id as 学号, s_name as 姓名, c_name as 课程名, sc_score as 分数
                  from student a, course b, score c
                  where a.s_id = c.s_id
                  and b.c_id = c.c_id
                  union
                  select s_id,'小计:','平均分:',avg(sc_score)
                  from score
                  group by s_id
                  union
                  select s_id,'小计:','总分:',sum(sc_score)
                  from score
                  group by s_id
                  复制

                  复制




                  文章转载自全栈精英,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                  评论