sql內外鏈結,交叉連線

2021-08-11 09:21:06 字數 3752 閱讀 9375

實際的專案,存在多張表的關聯關係。不可能在一張表裡面就能檢索出所有資料。如果沒有表連線的話,那麼我們就需要非常多的操作。比如需要從a表找出限制性的條件來從b表中檢索資料。不但需要分多表來操作,而且效率也不高。比如書中的例子:

複製**

**如下:

select fid

from t_customer

where fname='mike'

這個sql語句返回2,也就是姓名為mike 的客戶的fid值為2,這樣就可以到t_order中檢索fcustomerid等於2 的記錄:

複製**

**如下:

select fnumber,fprice

from t_order

where fcustomerid=2

下面我們詳細來看看表連線。表連線有多種不同的型別,有交叉連線(cross join)、內連線(inner join)、外連線(outter join)。

(1)內連線(inner join):內連線組合兩張表,並且只獲取滿足兩表連線條件的資料。

複製**

**如下:

select o.fid,o.fnumber,o.fprice,

c.fid,c.fname,c .fage

from t_order o join t_customer c

on o.fcustomerid= c.fid

注:在大多數資料庫系統中,inner join中的inner是可選的,inner join 是預設的連線方式。

在使用表連線的時候可以不侷限於只連線兩張表,因為有很多情況下需要聯絡許多表。例如,t_order表同時還需要連線t_customer和t_ordertype兩張表才能檢索到所需要的資訊,編寫如下sql語句即可:

複製**

**如下:

select o.fid,o.fnumber,o.fprice,

c.fid,c.fname,c .fage

from t_order o join t_customer c

on o.fcustomerid= c.fid

inner join t_ordertype

on t_order.ftypeid= t_ordertype.fid

(2)交叉連線(cross join):交叉連線所有涉及的表中的所有記錄都包含在結果集中。可以採用兩種方式來定義交叉連線,分別是隱式和顯式的連線。

下面看看隱式的例子:

複製**

**如下:

select t_customer.fid, t_customer.fname, t_customer.fage,

t_order.fid, t_order.fnumber, t_order.fprice

from t_customer, t_order

where 

t_customer.fid = t_order.fid

使用顯式的連線則需要使用cross join,例子如下:

複製**

**如下:

select t_customer.fid, t_customer.fname, t_customer.fage,

t_order.fid, t_order.fnumber, t_order.fprice

from t_customer

cross join t_order

where 

t_customer.fid = t_order.fid

(3)外連線(outter join):內部連線只獲取滿足連線條件的資料,而對於外部連線來說,主要是解決這樣的一種場景。滿足條件的資料檢索出來,這個沒有疑問,外部連線還會檢索另一部分資料,那就是將不滿足條件的資料以null來填充。先來看一下外連線的分類:左外部連線(left outer join)、右外部連線(right outer join)和全外部連線(fullouter join)。

i、左外部連線(left outer join):前頭也說了,將不滿足條件的資料以null來填充。那麼具體是哪些需要以null來填充呢,對於左外連線來說的話,連線條件當中,如果出現滿足條件的左表的資料在右表中沒有相應匹配時,需要把相應的右表字段填充null值。也就是說左外部連線的主體是左表,右表來配合。

複製**

**如下:

select o.fnumber,o.fprice,o.fcustomerid,

c.fname,c.fage

from t_order o

left outer join t_customer c

on o.fcustomerid=c.fid

注:如果使用左外部連線的話,通過where語句能過濾其中不符合的資料

複製**

**如下:

select o.fnumber,o.fprice,o.fcustomerid,

c.fname,c.fage

from t_order o

left outer join t_customer c

on o.fcustomerid=c.fid

where o.fprice>=150

ii、右外部連線(right outer join):右外部連線與左外連部接相反,將會被填充null值的是左表的字段。也就是說右外部連線的主體是右表,左表來配合。

複製**

**如下:

select o.fnumber,o.fprice,o.fcustomerid,

c.fname,c.fage

from t_order o

right outer join t_customer c

on o.fcustomerid=c.fid

注:同左外連線一樣,可以使用where語句進行過濾

iii、全外部連線(fullouter join):全外部連線是左外部連線和右外部連線的合集。也就是既包括左外部連線的結果集,也包括右外部連線的結果集。

複製**

**如下:

select o.fnumber,o.fprice,o.fcustomerid,

c.fname,c.fage

from t_order o

full outer join t_customer c

on o.fcustomerid=c.fid

其結果相當於:

複製**

**如下:

select o.fnumber,o.fprice,o.fcustomerid,

c.fname,c.fage

from t_order o

left outer join t_customer c

on o.fcustomerid=c.fid

union

select o.fnumber,o.fprice,o.fcustomerid,

c.fname,c.fage

from t_order o

right outer join t_customer c

on o.fcustomerid=c.fid

l 兩張連線的表中名稱和型別完全一致的列作為條件,例如emp和

dept

表都存在

deptno

列,並且型別一致,所以會被自然連線找到!

當然自然連線還有其他的查詢條件的方式,但其他方式都可能存在問題!

select * from emp natural join dept;   內連線

select * from emp natural left join dept;   左連線

select * from emp natural right join dept;   右連線

六 SQL 表連線 交叉連線

與內連線比起來,交叉連線非常簡單,因為它不存在on子句。交叉連線會將涉及到的所有表中的所有記錄都包含在結果集中。交叉連線,定義方式分為隱式連線和顯式連線。隱式連線 只要在select語句的from語句後將要進行交叉連線的表名列出即可,這種方式可以被幾乎任意資料庫系統支援。如 將t customer表...

SQL內外左右交叉連線

概念 根據兩個表或多個表的列之間的關係,從這些表中查詢資料。目的 實現多個表查詢操作。一般是用作關聯兩張或兩張以上的資料表時用的。看起來有點抽象,我們舉個例子,做兩張表 學生表 t student 和班級表 t class sql 92標準所定義的from子句的連線語法格式為 from join t...

內連線 外連線 自連線 交叉鏈結

資料表的連線有 1 內連線 自然連線 只有兩個表相匹配的行才能在結果集中出現 2 外連線 包括 1 左外連線 左邊的表不加限制 2 右外連線 右邊的表不加限制 3 全外連線 左右兩表都不加限制 3 自連線 連線發生在一張基表內 內連線 一 內連線 內連線查詢操作列出與連線條件匹配的資料行,它使用比較...