sql server 的T-SQL 学习笔记(七)


/************************* 多表连接查询 2017-7-21 13:51:26*******************************/

-- 多表了解查询
-- 多表连接的类型
-- 多表连接的综合运用

  select * from Student , ClassRoom where Student.classId = ClassRoom.classId
  select studentId ,studentName ,borthDay,studentScore,studentAddress,studentSex,className from Student,ClassRoom where Student.classId = ClassRoom.classId
  select studentId ,studentName ,borthDay,studentScore,studentAddress,studentSex,c.className from Student as s,ClassRoom as c where s.classId = c.classId

  -- 查询学生ID 姓名 班级名称 科目编号 成绩
  select 
    studentId as '学生ID',
    studentName as '学生姓名' ,
    c.className as '班级名称',
    s.courseId as '课程科目编号',
    studentScore as '成绩' 
  from 
    Student as s , 
    ClassRoom as c
  where 
    s.classId = c.classId 


  -- 查询学生ID 姓名 班级名称 科目名称 成绩
  select 
    studentId as '学生ID',
    studentName as '学生姓名' ,
    c.className as '班级名称',
    cou.courseName as '课程科目编号',
    studentScore as '成绩' 
  from 
    Student as s,
    ClassRoom as c,
    Course as cou
  where
    s.classId = c.classId
    and s.courseId = cou.courseId

  -- 查询学生id,姓名 班级名称 科目名称 成绩 显示不及格或者及格
  select 
    s.studentId as '学生id',
    s.studentName as '学生姓名',
    c.className as '班级名称',
    cou.courseName as '科目名称',
    s.studentScore as '分数'
  from 
    Student as s,
    ClassRoom as c,
    Course as cou
  where 
    s.classId = c.classId
    and s.courseId = cou.courseId

 -- ************************ 内连接 **********************
   -- 内连接也称之为等同连接 返回两个表中所有相匹配的数据
   --  select .....from table_name1 [inner] join table_name2 on  <表达式>
   -- 查询学生id 姓名 班级名称
     select s.studentId,s.studentName,c.className from Student as s inner join ClassRoom as c on s.classId = c.classId
   -- 查询学生ID 姓名 班级名称 科目编号 成绩(两张表内连接)
     select 
       s.studentId ,s.studentName ,c.className,s.courseId ,s.studentScore 
     from Student as s  
       inner join ClassRoom as c on s.classId = c.classId
   -- 查询学生id 姓名 班级名称 科目名称,成绩 (三表及三表以上内连接) 不及格人数
     select 
       s.studentId ,s.studentName,c.className,cou.courseName ,s.studentScore 
     from Student as s 
       inner join ClassRoom as c on s.classId = c.classId 
       inner join Course as cou on s.courseId = cou.courseId
     where s.studentScore < 60


-- ******************** 外连接 *********************************
  -- 填补没有的数据 (eg:学生考试缺考时候打印成绩表)
  -- 外连接就是在满足表连接关系的情况下不但可以查找出匹配的数据,还可以包含左表、右表或者两表的数据
    -- 左外连接
    -- 右外连接
    -- 全外连接
  -- 将inner修改为outer 同时outer可以省略

  -- 左外连接 left [outer] join
    select * from ClassRoom as c left outer join Student as s on s.classId = c.classId
  -- 右外连接 right [outer] join
    select * from ClassRoom as c right outer join Student as s on s.classId = c.classId 
  -- 全外连接 full [outer] join
    select * from ClassRoom as c full outer join Student as s on c.classId = s.classId

-- *********** union 运算符***************
  -- union 运算符进行集合的并运算
  -- union 拼接的列相同
  -- union 拼接的列属性相同
  -- union 可以用在相同的表 也可以用在不同的表
    -- union all 
    select * from Student where studentId < 4
      union all select * from Student where studentId < 7
    -- union 
    select * from Student where studentId < 4
      union select * from Student where studentId < 7
   -- 拼接的列相同(可以添加自定义的)

     select studentId ,studentName from Student where studentId <3
       union 
       select 111,'chencongchencong' 

你可能感兴趣的:(SQL-Server)