数据库(七)之连接

基本连接

  用户在进行基本连接操作时,可以遵循以下基本原则:

  • select子句列表中,每个目标列前都要加上基表名称
  • from子句应包括所有使用得基表
  • where子句应定义一个等同连接

  例子:

select A.姓名, A.性别, A.出生日期, A.民族, B.班级名, A.家庭住址
from 学生信息 A, 班级信息 B
where A.所属班级 = B.班级编号

内连接

select select_list
from table1 inner join table2 [ on join_conditions]
[where search_conditions]
[order by order_expression]

例子:

select A.班级名,A.班级人数,  B.姓名, B.联系方式
from 班级信息 A inner join 辅导员信息 B
on A.辅导员 = B.辅导员信息
select A.班级名, A.班级人数, B.姓名, B.联系方式
from 班级信息 A inner join 辅导员信息 B
on A.辅导员 = B.辅导员编号 and B.性别 = ‘女’
select A.班级名, A.班级人数, B.姓名, B.联系方式
from 班级信息 A inner join 辅导员信息 B
on A.辅导员 = B.辅导员编号
where B.性别 = ''

外连接

  内连接消除与另一个表的任何不匹配的行,而外连接会返回from子句中提到得至少一个表或视图中的所有行,只要这些行符合任何搜索条件。

  因为,在外连接中参与连接的表有主从之分,以主表的每行数据去匹配从表中的数据行,以主表的每行数据去匹配从表中的数据行,如果符合连接条件,则直接返回到查询结果中;如果主表中的行在从表中没有找到匹配的行,主表的行仍然保留,并返回到查询结果中,相应的从表中的行中被填上空值后也返回到查询结果中。

左外连接

select A.班级名, A.班级人数, B.姓名, B.联系方式
from 班级信息 A left outer join 辅导员信息 B
on A.辅导员 = B.辅导员编号
select A.班级名, A.班级人数, B.姓名, B.联系方式
from 班级信息 A left outer join 辅导员信息 B
on A.辅导员 = B.辅导员编号 and A.班级人数 > 20
select A.班级名, A.班级人数, B.姓名, B.联系方式
from 班级信息 A left outer join 辅导员信息 B
on A.辅导员 = B.辅导员编号
where A.班级人数 > 20

右外连接

select A.班级名, A.班级人数, B.姓名, B.联系方式
from 班级信息 A right outer join 辅导员信息 B
on A.辅导员 = B.辅导员编号

全连接

select A.班级名, A.班级人数. B.姓名, B.联系方式
from 辅导员信息 b full outer join 班级信息 a
on A.辅导员 = B.辅导员编号
select A.班级名, A.班级人数, B.姓名, B.联系方式
from 班级信息 a full outer join 辅导员信息 b
on A.辅导员 = B.辅导员编号

 交叉连接

  交叉连接不带where子句,它返回被连接的两个表所有数据行的笛卡儿积,返回到结果集合中的数据行数等于第一个表中符合查询条件的数据行数乘以第二个表中符合查询的数据行数。

  语法格式:

select select_list
from table1 cross join table2
where search_conditions
order by order_expression

例子:

select A.班级名, A.班级人数, B.姓名
from 班级信息 A cross join 辅导员信息 B
where A.辅导员 = B.辅导员编号

自连接

select A.班级名, A.班级人数, B.班级名
from 班级信息 A, 班级信息 B
where A.班级人数 = B.班级人数
select A.班级名, A.班级人数, B.班级名
from 班级信息 A. 班级信息 B
where A.班级人数 = B.班级人数 and A.班级编号 <>B.班级编号
select A.班级名, A.班级人数, B.班级名
from 班级信息 A, 班级信息 B
where A.班级人数 = B.班级人数 and A.班级编号 <> B.班级编号 and A.班级名 = '计算机科学与技术2班'

联合查询
  语法格式:

select select_list
from table_source
where search_conditions
{union[all]
select select_list
from table_source
where search_conditions
[order by order_expression]

  注意:

  使用union查询时,连接的两个结果集必须在其目标列表中有相同数目的表达式,且数据内容尽量保持一致。

  例子:

select A.成绩信息, A.分数, B.姓名
from 成绩信息 A, 学生信息 B
where A.学生编号 = B.学号 and A.课程编号 = '2' and A.考试编号 = '0801'
union
select '', avg(分数), '合计'
from 成绩信息 A, 学生信息 B
where A.学生编号 = B.学号 and A.课程编号 = '2' and A.考试编号 = '0801'
union
select '', sum(分数), '合计'
from 成绩信息 A, 学生信息 B
where A.学生编号 = B.学号 and A.课程编号 = '2' and A.考试编号 = '0801'
order by 分数

使用子查询

select A.成绩编号, A.分数, B.姓名
from 成绩信息 A, 学生信息 B
where A.学生编号 = B.学号 and A.课程编号 = '2' and A.考试编号 = '0801'
and A.分数 < (
select avg(分数) from 成绩信息 A, 学生信息 B
where A.学生编号 = B.学号 and A.课程编号 = '2' and A.考试编号 = '0801'
select A.成绩编号, A.分数, B.姓名
from 成绩信息 A, 学生信息 B
where A.学生编号 = B.学号 and A.课程编号 = '2' and A.考试编号 =0801and B.学号 in(
select 学号 from 班级信息 A, 学生信息 B
where A.学生编号 = B.学号 and A.班级编号 =20181112’
)

 嵌套子查询

select A.成绩编号, A.分数, B.姓名
from 成绩信息A, 学生信息 B
where A.学生编号 = B.学号 and A.课程编号 =2and A.考试编号= '0801'
and B.学号 in(
    select 学号 from 班级信息 A, 学生信息 B
    where A.班级编号 = B.所属班级 and A.辅导员 = (
        select 辅导员编号 from 辅导员信息 where 姓名 = '王艳'
    )
)

xml查询

create table student
(
    s_id int,
    s_data xml         
)    

insert into student values(
1,
 ' <学生信息><姓名>刘倩</姓名><性别>女</性别><班级>计算机科学与技术1班</班级><职位>班长</职位></学生信息>'
)

select * from student
declare @data xml
set @data = (select s_data from student where s_id = 1)
select @data.query('学生信息/姓名' 姓名, @data.query('学生信息/性别'), 

for xml子句
  通过for xml子句并指定模式可以将从数据库系统的表中检索出来的数据自动表示成xml格式。有多种显示模式,如RAW模式,AUTO模式,EXPLICIT模式和PATH模式等。

  例子:

select A.班级名, A.班级人数, B.姓名, B.联系方式
from 班级信息 A, 辅导员信息 B
where A.辅导员 = B.辅导员编号
select A.班级名, A.班级人数, B.姓名, B.联系方式
from 班级信息 A, 辅导员信息 B
where A.辅导员 = B.辅导员编号
for xml raw
select A.班级名, A.班级人数, B.姓名, B.联系方式
from 班级信息 A, 辅导员信息 B
where A.辅导员 = B.辅导员编号
for xml auto

exists关键字查询

select a.* from 成绩信息 a
where exists(select * from 考试安排 b where b.考试编号 = a.考试编号 and b.考试编号 = '0801')

declare @username varchar(20)
declare @pwd varchar(20)
set @username = '2018040102'
set @pwd = 'zhao'
if exists(select * from 学生信息 where 学号 = @username and 姓名 = @pwd)
    print '登陆成功'
else
    print '登陆失败'

交查询intersect

select a.成绩信息, a.分数, b.姓名 from 成绩信息 a, 学生信息 b
where a.学生编号 = b.学号 and a.课程编号 = '2' and a.考试编号 = '0801'

select top 10 a.成绩编号, a.分数, b.姓名 from 成绩信息 a, 学生信息 b
where a.学生编号 = b.学号 and a.考试编号 =0801order by a.分数 desc

select a.成绩信息, a.分数, b.姓名 from 成绩信息 a, 学生信息 b
where a.学生编号 = b.学号 and a.课程编号 = 2 and a.考试编号 = '0801'
intersect
select a.成绩信息, a.分数, A.姓名 from 
(
    select top 10 a.成绩信息, a.分数, b.姓名 from 成绩信息 a, 学生信息 b
    where a.学生编号 = b.学号 and a.考试编号 = '0801' order by a.分数 desc
) A

差查询except

select a.成绩信息, a.分数, b.姓名 from 成绩信息 a, 学生信息 b
where a.学生编号 = b.学号 and a.课程编号 = 2 and a.考试编号 = '0801'
except
select a.成绩信息, a.分数, A.姓名 from 
(
    select top 10 a.成绩信息, a.分数, b.姓名 from 成绩信息 a, 学生信息 b
    where a.学生编号 = b.学号 and a.考试编号 = '0801' order by a.分数 desc
)A

 

     

 

posted @ 2018-03-19 14:02  去伪存真  阅读(233)  评论(0编辑  收藏  举报