SQL中內聯接與外聯接小結

2021-09-24 14:31:35 字數 3541 閱讀 5577

內聯接與外聯接的區別通過例項來說明是最清楚的了,下面先準備好測試環境:

create

database test

go

create

table t1 (deptid

int,uname

char(10))

create

table t2 (deptid

int,memo

char(50))

insert

into t1

values(1,

'john')

insert

into t1

values(2,

'tom')

insert

into t1

values(3,

'michal')

insert

into t2

values(1,

'human resources department')

insert

into t2

values(2,

'general accounting department')

insert

into t2

values(4,

'engineering department')

insert

into t2

values(5,

'sales department')

下面依次執行下列語句:

select *

from t1,t2

where t1.deptid=t2.deptid

select *

from t1

inner

join t2

on t1.deptid=t2.deptid

select *

from t1

left

join t2

on t1.deptid=t2.deptid

select *

from t1

right

join t2

on t1.deptid=t2.deptid

select *

from t1

full

join t2

on t1.deptid=t2.deptid

select *

from t1

cross

join t2

語句1和2結果均為:

deptid   

uname  

deptid   

memo

1john 

1human resources department

2tom 

2general accounting department  

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

可見內聯接與where子句效果一樣。

語句3(左聯)執行結果為:

deptid   

uname 

deptid  

memo

1john

1human resources department

2tom

2general accounting department  

3

michael

null 

null 

語句4(右聯)執行結果為:

deptid  

uname  

deptid   

memo

1john

1 human resources department

2tom

2general accounting department

null

null

4

engineering department

null

null

5

sales department

語句5(全聯)執行結果為:

deptid  

uname 

deptid

memo  

1john

1 human resources department

2tom

2general accounting department

null

null

4

engineering department

null

null

5

sales department

3

michael

null 

null 

語句6(交叉聯接,注意該語句是沒有on子句的!)執行結果為:

deptid     uname   deptid      memo

1   john          1    human resources department                       

1   john          2    general accounting department                    

1   john          4    engineering department                           

1   john          5    sales department                                 

2   tom             1    human resources department                       

2   tom             2    general accounting department                    

2   tom             4    engineering department                           

2   tom             5    sales department                                 

3   michael        1    human resources department                       

3   michael        2    general accounting department                    

3   michael        4    engineering department                           

3   michael        5    sales department   

hive內聯接和外聯接

hql很多語句和sql有相似之處,下面用例子快速了解內外聯接的用法 在多表操作的時候,經常會遇到需要的資料,一部分存在a表,一部分存在b表,或者存在更多的表中。而我們可以從這些表的關係進行聯接,下面建立兩個表進行例項演示 首先建立乙個學生資訊表,有id,s name,c name三個屬性 hive ...

外聯接小結

連環多表外聯接 select from circuit c,device d,resgroup g,portinfo p,devaddr a,device b,devaddr ba where c.circuitid cirid and c.changetype 0 or c.changetype ...

資料庫內聯接 左外聯接 右外聯接和全聯接

首先給出兩張表用於之後的舉例 stuno stuname gradeid 1001張三1 1002李四2 1003王五3 1004 西楚霸王 1000 gradeid grade 1一年級 2二年級 3三年級 4四年級 顯示結果為符合條件的多個表間的交集。例 select from grade g,...