• 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
  • 相关阅读:
    制造业接入物联网的4大优势
    一篇文章读懂什么是串口通信及其工作原理
    4G工业路由器在仓储物流中的应用
    4G工业路由器应用于远程医疗设备监控
    串口服务器在饮料自动化生产线的应用
    PLC和串口服务器在数字化工厂中的作用
    LoRa集中器在石油勘探等领域的应用解决方案
    4G模块为基础的物联网在精准农业中的应用
    4G工业路由器在垃圾和废水处理的应用案例
    HDU 6205 2017沈阳网络赛 思维题
  • 原文地址:https://www.cnblogs.com/qiqiBoKe/p/2791628.html
Copyright © 2020-2023  润新知