• SQL重点复习


    -----------------------------------
    --1使用子查询实现命题查询出所有没有参加考试的同学的学生编号,姓名。
    use MySchool
    select * from Student
    select * from Score


    select sNo,sName from Student
    where sId not in(select studentId from Score)


    --2使用联接重做:查询出所有没有参加考试的同学的学生编号,姓名。
       
       select sNo,sName from(
       select sNo,sName,scoreId from Student
       left outer join Score on Student.sId=Score.studentId )
       as tab1 where scoreId is null
       
    --3查询所有英语及格的学生姓名、年龄及成绩


      select sName,sAge,english,math  from Student
      inner join Score on Student.sId=Score.studentId
      where english>60 and math>60
      
    --4查询所有参加考试的(english分数不为null)学生姓名、年龄及成绩


       select sName,sAge,english from Student
       inner join Score on Student.sId=Score.studentId
       where english is not null


    --5查询所有学生(报考的和未报考的)的学生姓名、年龄、成绩,如果报考了
    --但是没有参加考试显示缺考,如果小于english&math小于60分显示不及
    --如果没有报考显示没有报考(添加两列 ,“是否报考”,“是否合格”)


     select sName,sAge,
     english=case
      when english is null then '缺考'
      else CONVERT(nvarchar(6),english)
      end,
      math=case
      when math is null then '缺考'
      else CONVERT(nvarchar(6),math)
      end,
      是否报考=case
       when scoreId is null then '未报考'
       else '已报考'
       end,
       是否合格=case
       when english>60 and math>60 then '合格'
       else '不合格'
       end
      from Student
     left outer join Score on Student.sId=Score.studentId
     
    --6新建 临时表(#MyStudents,包含2个字段分别为sName、sAge)
    --并将Mystudents中的相应数据copy其中。
     
     create table #Mystudents
     (
       sName nvarchar(50),
       sAge int
     )
     
     insert into #Mystudents (sName,sAge) 
     select sName,sAge from Student
     
     select * from #Mystudents
     
    --7定义表变量、插入数据并查询:
    --表变量在会话结时,自动释放掉
      declare @tabBL table (col1 int,col2 nvarchar(50))
      insert into @tabBL
      select 1,'A' union
      select 2,'B'
    select * from @tabBL


    --8新建视图,修改视图,删除视图


     create view shitu
     as
      select sName,sAge,
     english=case
      when english is null then '缺考'
      else CONVERT(nvarchar(6),english)
      end,
      math=case
      when math is null then '缺考'
      else CONVERT(nvarchar(6),math)
      end,
      是否报考=case
       when scoreId is null then '未报考'
       else '已报考'
       end,
       是否合格=case
       when english>60 and math>60 then '合格'
       else '不合格'
       end
      from Student
     left outer join Score on Student.sId=Score.studentId


     select * from shitu
     
     update Student set sName='西施' where sName='华佗'
     
     drop view shitu
  • 相关阅读:
    Leo程序员羊皮卷文摘(更新ing)
    ubuntu下的yuv播放器
    浏览器之一
    海量数据处理常用思路和方法(zh)
    我本将心向明月,奈何明月照沟渠
    转载光纤通信之父
    重装系统或是更换电脑之后,Foxmail的恢复
    关于录制Linux视频
    Linux之路(原发表于07年,现在搬到博客)
    Gentoo安装 miniCD+stage3
  • 原文地址:https://www.cnblogs.com/qiqiBoKe/p/2791629.html
Copyright © 2020-2023  润新知