有勇气的牛排博客

SQL Sever联接查询

有勇气的牛排 1214 数据库 2021-08-26 21:57:34

介绍

联接查询:是有一个笛卡尔乘积运算再加一个选取运算构成的查询。
首先用笛卡尔乘积完成对两个数据集合的乘运算,然后对生成的结果集合进行选取运算,确保只把分别来自两个数据集合并且具有重叠部分的行合并在一起。

联接的全部意义在于水平方向上合并两个数据集合,并产生一盒新的结果集合。

联接可分为以下几类:内部联接、外部联接、交叉联接

20201018221115553.png

20201018221141240.png

20201018230046124.png

1. 内部联接

内部联接是使用比较运算符比较要联接列中的值得联接。内连接也叫联接,最早被称为普通联接或自然联接。
内联接是从结果中删除其他被联接表中没有匹配行的所有行,所以内联接可能会丢失信息。

select * from SC join Student on Student.Sno = SC.Sno

2020101822102671.png

2. 外部联接

外部联接则扩充了内联接的功能,会吧内联接中删除表源中的一些保留下来,由于保留下来的行不同,可将外部联接分为 左向外部联接右向外部联接完整外部联接

2.1 左向外联接

语法

select fieidlist from table1 left join table2 on table1.comlumnn=table2.column

参数说明:

参数 说明
fieldlist 搜索条件
table1 [inner] join table2 将table1表与table2表进行外部联接
table1.column=table2.column table1表中与table2表中相同的列

eg. 把Studnet表和SC表左外连接,第二个表SC有不满足联接条件的行,则用NULL表示 。

select * from Student left join SC on Student.Sno=SC.Sno

20201018223444372.png

解析:这里出现NULL说明SC表中没有Sno等于4和6的

2.2 右向外联接

右外向联接使用 right jion 进行联接,是左向外联接的反向联接。将返回右表的所有行。如果右表的某一行在左表中没有匹配行,则将为坐标返回空值。

语法:

select fieldlist from table1 right join table2 on table1.column=table2.column

eg. 把SC表和Course表右外联接,第一个表SC有不满足联接条件的行,则用NULL表示。

select * from SC right join Course on Course.Cno=SC.Cno

这里右外联接的是Course表,所以Course表的数据完整

20201018224927384.png

2.3 完整外联接

完整外连接使用 full join 进行联接,将返回左表和右表中的所有行。当某一行在另一个表中没有匹配时,另一个表的选择列表将包含空值。如果表之间有匹配行,则将整个结果集行包含基表的数据值。

语法:

select fieldlistfrom table1 full join table2 on table1.column=table2.column

eg. 把SC表和Course表完整外部联接,显示两个表中所有的行。

select * from SC full join Course on Course.Cno=SC.Cno

3. 交叉联接

交叉联接使用 cross join 进行联接,没有where子句的交叉联接将产生联接所涉及的表的笛卡尔积。第一个表的行数乘以第二个表的行数等于笛卡尔积结果集得大小。

交叉联接中列和行的数量是这样计算的:

  • 交叉联接中的列=原表中列的数量总和(想加)。
  • 交叉联接中的行=原表中行数的积(相乘)。

语法:

select fieldlist from table1 cross join table2

其中,忽略 on 方法来创建交叉联接。

eg. 把 Student 表和 Course 表进行交叉联接。

select * from Student cross join Course

4. 联接多表

4.1 在where子句中联接多表

在 from 子句中写联接多个表的名称,然后将任意两个表的联接条件分别写在 where 子句后。

语法:

select fieldlist from table1, table2, table3 ... where table1.column=table2.column and table2.column=table3.column and ...

eg. 把 Student 表、Course 表和 SC 表,这3个表在 where 子句中联接。

select * from Student,Course,SC where Student.Sno=SC.Sno and SC.Cno=Course.Cno

20201018234208455.png

4.2 在 from 子句中联接多表

在 from 子句中联接多个表示内部联接的扩展。
语法:

select fieldlist from table1 join table2 join table3... on table1.column=table2.column and tbale2.column=table3.column

eg. 把 Student表、Course表 和 SC表,这3个表在from子句中联接。

select * from Student join SC join Course on SC.Sno=Course.Cno on Student.Sno=SC.Sno

20201018235046220.png

5. 使用 case 函数进行查询

case 函数用于计算条件列表并返回多个可能结果表达式之一。
case函数具有以下两种格式:

  • 简单case函数将某个表达式与一组简单表达式进行比较以确定结果。
  • case搜索函数计算一组不二表达式以确定结果。

简单 case 函数的语法如下:

case input_expression when when_expression then result_expression [...n] [ else else_result_expression ] end

case 搜索函数的语法如下:

case when Boolean_expression then result_expression [...n] [ else else_result_expression ] end

在 select 语句中,简单 case 函数仅检查是否相等,而不进行其他比较。本实例使用 case 函数更改产品系列类别的显示,以使这些类别更容易理解。

select ProductNumber, Category = case productLine when 'R' then 'Rpad' when 'M' then 'Mountain' when 'T' then 'Touring' when 'S' then 'Other sale items' else 'Not for sale' end Name from Production.Product order by ProductNumber

eg. 查询SC表中的Sno和Cno,如果分数 >=90为优秀,>=80为良好

select Sno,Cno,等级=case when Grade >=90 then '优秀' when Grade >=80 then '良好' else '不及格' end from SC

20201020130303800.png

使用 case 语句更新

使用 case 语句更新学生信息,使所有男生年龄减 1 ,所有女生年龄加 1 ,

update Student set Sage= case when Sex='男' then Sage-1 when Sex = '女' then Sage+1 end

20201020131239498.png


留言

专栏
文章
加入群聊