Posted on 2008-12-03 09:22
Prayer 閱讀(349)
評論(0) 編輯 收藏 引用 所屬分類:
數據庫,SQL
左連接/右連接 實例
有兩個表:
table1
id
|
name
|
password
|
1
|
aaa
|
a
|
2
|
bbb
|
b
|
3
|
ccc
|
c
|
table2
id
|
power
|
1
|
111000
|
2
|
000111
|
左連接sql語句:select table1.id,table1.name,table2.power from table1 left join table2 on table1.id = table2.id
得到如下結果:
id
|
name
|
power
|
1
|
aaa
|
000111
|
2
|
bbb
|
111000
|
3
|
ccc
|
(null)
|
右連接sql語句:select table1.id,table1.name,table2.power from table1 right join table2 on table1.id = table2.id
得到如下結果:
id
|
name
|
power
|
1
|
aaa
|
000111
|
2
|
bbb
|
111000
|
結果說明:左/右連接查詢實際上是指定以哪個表的數據為準。
左連接是只要左邊的表中有記錄,數據就能檢索出來,而右邊表中的記錄必須在
左邊表中有記錄才能檢索出來;右連接同理。
|