1. 程式人生 > >數據庫(七)之連接

數據庫(七)之連接

font eat 模式 pwd color 聯合 空值 sele ont

select A.班級名, A.班級人數, B.姓名, B.聯系方式
from 班級信息 A, 輔導員信息 B
where A.輔導員 = B.輔導員編號
for xml raw

基本連接

  用戶在進行基本連接操作時,可以遵循以下基本原則:

  • 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

     

數據庫(七)之連接