這一片海,源于那一瓢水

          sql join的用法(轉)

          關于sql語句中的連接(join)關鍵字,是較為常用而又不太容易理解的關鍵字,下面這個例子給出了一個簡單的解釋 --建表table1,table2:
          create table table1(id int,name varchar(10))
          create table table2(id int,score int)
          insert into table1 select 1,'lee'
          insert into table1 select 2,'zhang'
          insert into table1 select 4,'wang'
          insert into table2 select 1,90
          insert into table2 select 2,100
          insert into table2 select 3,70
          如表
          -------------------------------------------------
           table1  | table2  |
          -------------------------------------------------
          id  name |id  score |
          1  lee |1  90 |
          2  zhang |2  100 |
          4  wang |3  70 |
          -------------------------------------------------

          以下均在查詢分析器中執行

          一、外連接
          1.概念:包括左向外聯接、右向外聯接或完整外部聯接

          2.左連接:left join 或 left outer join
          (1)左向外聯接的結果集包括 LEFT OUTER 子句中指定的左表的所有行,而不僅僅是聯接列所匹配的行。如果左表的某行在右表中沒有匹配行,則在相關聯的結果集行中右表的所有選擇列表列均為空值(null)。
          (2)sql語句
          select * from table1 left join table2 on table1.id=table2.id
          -------------結果-------------
          id name id score
          ------------------------------
          1 lee 1 90
          2 zhang 2 100
          4 wang NULL NULL
          ------------------------------
          注釋:包含table1的所有子句,根據指定條件返回table2相應的字段,不符合的以null顯示

          3.右連接:right join 或 right outer join
          (1)右向外聯接是左向外聯接的反向聯接。將返回右表的所有行。如果右表的某行在左表中沒有匹配行,則將為左表返回空值。
          (2)sql語句
          select * from table1 right join table2 on table1.id=table2.id
          -------------結果-------------
          id name id score
          ------------------------------
          1 lee 1 90
          2 zhang 2 100
          NULL NULL 3 70
          ------------------------------
          注釋:包含table2的所有子句,根據指定條件返回table1相應的字段,不符合的以null顯示

          4.完整外部聯接:full join 或 full outer join
          (1)完整外部聯接返回左表和右表中的所有行。當某行在另一個表中沒有匹配行時,則另一個表的選擇列表列包含空值。如果表之間有匹配行,則整個結果集行包含基表的數據值。
          (2)sql語句
          select * from table1 full join table2 on table1.id=table2.id
          -------------結果-------------
          id name id score
          ------------------------------
          1 lee 1 90
          2 zhang 2 100
          4 wang NULL NULL
          NULL NULL 3 70
          ------------------------------
          注釋:返回左右連接的和(見上左、右連接)

          二、內連接
          1.概念:內聯接是用比較運算符比較要聯接列的值的聯接

          2.內連接:join 或 inner join

          3.sql語句
          select * from table1 join table2 on table1.id=table2.id
          -------------結果-------------
          id name id score
          ------------------------------
          1 lee 1 90
          2 zhang 2 100
          ------------------------------
          注釋:只返回符合條件的table1和table2的列

          4.等價(與下列執行效果相同)
          A:select a.*,b.* from table1 a,table2 b where a.id=b.id
          B:select * from table1 cross join table2 where table1.id=table2.id  (注:cross join后加條件只能用where,不能用on)

          三、交叉連接(完全)

          1.概念:沒有 WHERE 子句的交叉聯接將產生聯接所涉及的表的笛卡爾積。第一個表的行數乘以第二個表的行數等于笛卡爾積結果集的大小。(table1和table2交叉連接產生3*3=9條記錄)

          2.交叉連接:cross join (不帶條件where...)

          3.sql語句
          select * from table1 cross join table2
          -------------結果-------------
          id name id score
          ------------------------------
          1 lee 1 90
          2 zhang 1 90
          4 wang 1 90
          1 lee 2 100
          2 zhang 2 100
          4 wang 2 100
          1 lee 3 70
          2 zhang 3 70
          4 wang 3 70
          ------------------------------
          注釋:返回3*3=9條記錄,即笛卡爾積

          4.等價(與下列執行效果相同)
          A:select * from table1,table2       

          轉載地址:http://www.cnblogs.com/yiki/archive/2007/01/04/611452.html

          posted on 2010-06-25 16:56 何方 閱讀(93) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          My Links

          Blog Stats

          常用鏈接

          留言簿

          隨筆檔案

          文章分類

          文章檔案

          相冊

          搜索

          最新評論

          主站蜘蛛池模板: 无为县| 湖口县| 克什克腾旗| 土默特右旗| 克山县| 黑山县| 南溪县| 迁安市| 阜南县| 临泉县| 清丰县| 惠来县| 会理县| 浠水县| 永修县| 鸡东县| 昌图县| 花莲市| 黄陵县| 会东县| 嘉定区| 庄浪县| 梧州市| 海城市| 牟定县| 建湖县| 泽库县| 赣州市| 萨嘎县| 西华县| 崇文区| 梨树县| 鱼台县| 家居| 额尔古纳市| 罗江县| 清水县| 尉犁县| 鄂温| 广河县| 安义县|