tinguo002

           

          left join加上where條件的困惑

          原文:http://hi.baidu.com/dianai2001/item/6c2ddd844a80ced45e0ec136
          2009-12-31 10:26

          left join加上where條件的困惑



          left join的困惑:一旦加上where條件,則顯示的結(jié)果等于inner join


          將where 換成 and 


          用where 是先連接然后再篩選  
          用and 是先篩選再連接


          數(shù)據(jù)庫(kù)在通過連接兩張或多張表來返回記錄時(shí),都會(huì)生成一張中間的臨時(shí)表,然后再將這張臨時(shí)表返回給用戶。


                在使用left jion時(shí),on和where條件的區(qū)別如下:


          1、 on條件是在生成臨時(shí)表時(shí)使用的條件,它不管on中的條件是否為真,都會(huì)返回左邊表中的記錄。


          2、where條件是在臨時(shí)表生成好后,再對(duì)臨時(shí)表進(jìn)行過濾的條件。這時(shí)已經(jīng)沒有l(wèi)eft
          join的含義(必須返回左邊表的記錄)了,條件不為真的就全部過濾掉。


                 假設(shè)有兩張表:


          表1 tab1:


          id size


          1 10


          2 20


          3 30


          表2 tab2:


          size name


          10 AAA


          20 BBB


          20 CCC



          兩條SQL:
          1、select * form tab1 left join tab2 on (tab1.size = tab2.size)
          where tab2.name=’AAA’
          2、select * form tab1 left join tab2 on (tab1.size =
          tab2.size and tab2.name=’AAA’)


          第一條SQL的過程:


          1、中間表
          on條件:
          tab1.size = tab2.size


          tab1.id    tab1.size    tab2.size     tab2.name


          1               10                   10               AAA


          2              20                     20             BBB


          2             20                      20               CCC


          3             30                    (null)              (null)


          2、再對(duì)中間表過濾
          where 條件:
          tab2.name=’AAA’


          tab1.id       tab1.size        tab2.size     tab2.name


          1                  10                  10              AAA



          第二條SQL的過程:


          1、中間表
          on條件:
          tab1.size = tab2.size and
          tab2.name=’AAA’
          (條件不為真也會(huì)返回左表中的記錄)


          tab1.id      tab1.size         tab2.size       tab2.name


          1               10                     10                   AAA


          2               20                   (null)               (null)


          3               30                    (null)                 (null)


               其實(shí)以上結(jié)果的關(guān)鍵原因就是left join,right join,full
          join的特殊性,不管on上的條件是否為真都會(huì)返回left或right表中的記錄,full則具有l(wèi)eft和right的特性的并集。 而inner
          jion沒這個(gè)特殊性,則條件放在on中和where中,返回的結(jié)果集是相同的。



          歡迎大家訪問我的個(gè)人網(wǎng)站 萌萌的IT人

          posted on 2013-09-23 16:09 一堣而安 閱讀(281) 評(píng)論(0)  編輯  收藏 所屬分類: oraclesql

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          收藏夾

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 平陆县| 横山县| 建湖县| 麻阳| 广宗县| 隆尧县| 梅州市| 乌拉特中旗| 司法| 巨野县| 当阳市| 龙胜| 南溪县| 星座| 东台市| 巴东县| 巫山县| 什邡市| 海宁市| 平原县| 汝州市| 南陵县| 舒兰市| 策勒县| 灵寿县| 曲周县| 崇州市| 左权县| 和龙市| 河津市| 大冶市| 新竹县| 会东县| 沭阳县| 丰台区| 鄂州市| 汝南县| 靖边县| 阳原县| 邯郸县| 盐池县|