2011年11月30日

          (轉貼)數據庫連接(內連接,外連接,交叉連接)

          數據庫連接分為:內連接,外連接(左、右連接,全連接),交叉連接
          文章地址 : http://www.zxbc.cn/html/20080527/51189.html
          轉載 
          內連接:把兩個表中數據對應的數據查出來 
          外連接:以某個表為基礎把對應數據查出來(全連接是以多個表為基礎) 
          student表 
          no name 
          1     a 
          2     b 
          3     c 
          4     d 
          grade表 
          no grade 
          1     90 
          2     98 
          3     95 
          內連接 inner join(查找條件中對應的數據,no4沒有數據不列出來) 
          語法:select * from student inner join grade on student.no = grade.no 
          結果 
          student.no name grade.no grade 
          1             a             1         90 
          2             b             2         98 
          3             c             3         95 
          左連接(左表中所有數據,右表中對應數據) 
          語法:select * from student left join grade on student.no = grade.no 
          結果: 
          student.no name grade.no grade 
          1                 a         1         90 
          2                 b         2         98 
          3                 c         3         95 
          4                 d     
          右連接(右表中所有數據,左表中對應數據) 
          語法:select * from student right join grade on student.no = grade.no 
          結果: 
          student.no name grade.no grade 
          1                 a         1         90 
          2                 b         2         98 
          3                 c         3         95 
          全連接 
          語法:select * from student full join grade on student.no = grade.no 
          結果: 
          no name grade 
          1     a     90 
          2     b     98 
          3     c     95 
          4     d 
          1     a     90 
          2     b     98 
          3     c     95 
          注:access 中不能直接使用full join ,需要使用union all 將左連接和右連接合并后才可以

          交叉連接
          將兩個表所有行組合,連接后的行數為兩個表行數的乘積(笛卡爾積)
          語法,借用上面的例子應該是
          select * from student cross join grade

          行數應該為12行 :
          no name grade 
          1     a     90 
          2     b     98 
          3     c     95 
          4     d  
          1     a     90 
          2     b     98 
          3     c     95 
          4     d 
          1     a     90 
          2     b     98 
          3     c     95 
          4     d 

          posted @ 2011-11-30 17:24 AK47 閱讀(499) | 評論 (0)編輯 收藏

          <2011年11月>
          303112345
          6789101112
          13141516171819
          20212223242526
          27282930123
          45678910

          導航

          統計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 南汇区| 望奎县| 晋中市| 广汉市| 开平市| 疏附县| 开封市| 邵阳市| 开原市| 湟源县| 兰考县| 酒泉市| 临西县| 伊川县| 高要市| 新津县| 沧源| 平乡县| 枣庄市| 始兴县| 西充县| 道孚县| 海南省| 新邵县| 滕州市| 商城县| 河池市| 宁海县| 敖汉旗| 思茅市| 阿图什市| 东方市| 化德县| 中卫市| 会昌县| 陕西省| 瑞金市| 工布江达县| 荔波县| 射阳县| 马山县|