左外連接,右外連接,全連接,內連接的各差異,以及何時用什么連接?


          SQL--JOIN之完全用法? ???
          ? ?? ?
          ? ?? ???
          ? ?? ???
          ? ?
          ? ?
          ??外聯接。外聯接可以是左向外聯接、右向外聯接或完整外部聯接。? ???
          ??在? ?FROM? ?子句中指定外聯接時,可以由下列幾組關鍵字中的一組指定:? ?
          ? ?
          ??LEFT? ?JOIN? ?或? ?LEFT? ?OUTER? ?JOIN。? ???
          ??左向外聯接的結果集包括? ?LEFT? ?OUTER? ?子句中指定的左表的所有行,而不僅僅是聯接列所匹配的行。如果左表的某行在右表中沒有匹配行,則在相關聯的結果集行中右表的所有選擇列表列均為空值。? ?
          ? ?
          ??RIGHT? ?JOIN? ?或? ?RIGHT? ?OUTER? ?JOIN。? ???
          ??右向外聯接是左向外聯接的反向聯接。將返回右表的所有行。如果右表的某行在左表中沒有匹配行,則將為左表返回空值。? ?
          ? ?
          ??FULL? ?JOIN? ?或? ?FULL? ?OUTER? ?JOIN。? ???
          ??完整外部聯接返回左表和右表中的所有行。當某行在另一個表中沒有匹配行時,則另一個表的選擇列表列包含空值。如果表之間有匹配行,則整個結果集行包含基表的數據值。? ?
          ? ?
          ??僅當至少有一個同屬于兩表的行符合聯接條件時,內聯接才返回行。內聯接消除與另一個表中的任何行不匹配的行。而外聯接會返回? ?FROM? ?子句中提到的至少一個表或視圖的所有行,只要這些行符合任何? ?WHERE? ?或? ?HAVING? ?搜索條件。將檢索通過左向外聯接引用的左表的所有行,以及通過右向外聯接引用的右表的所有行。完整外部聯接中兩個表的所有行都將返回。? ?
          ? ?
          ??Microsoft?? ?SQL? ?Server?? ?2000? ?對在? ?FROM? ?子句中指定的外聯接使用以下? ?SQL-92? ?關鍵字:? ???
          ? ?
          ??LEFT? ?OUTER? ?JOIN? ?或? ?LEFT? ?JOIN? ?
          ? ?
          ? ?
          ??RIGHT? ?OUTER? ?JOIN? ?或? ?RIGHT? ?JOIN? ?
          ? ?
          ? ?
          ??FULL? ?OUTER? ?JOIN? ?或? ?FULL? ?JOIN? ???
          ??SQL? ?Server? ?支持? ?SQL-92? ?外聯接語法,以及在? ?WHERE? ?子句中使用? ?*=? ?和? ?=*? ?運算符指定外聯接的舊式語法。由于? ?SQL-92? ?語法不容易產生歧義,而舊式? ?Transact-SQL? ?外聯接有時會產生歧義,因此建議使用? ?SQL-92? ?語法。? ?
          ? ?
          ??使用左向外聯接? ?
          ??假設在? ?city? ?列上聯接? ?authors? ?表和? ?publishers? ?表。結果只顯示在出版商所在城市居住的作者(本例中為? ?Abraham? ?Bennet? ?和? ?Cheryl? ?Carson)。? ?
          ? ?
          ??若要在結果中包括所有的作者,而不管出版商是否住在同一個城市,請使用? ?SQL-92? ?左向外聯接。下面是? ?Transact-SQL? ?左向外聯接的查詢和結果:? ?
          ? ?
          ??USE? ?pubs? ?
          ??SELECT? ?a.au_fname,? ?a.au_lname,? ?p.pub_name? ?
          ??FROM? ?authors? ?a? ?LEFT? ?OUTER? ?JOIN? ?publishers? ?p? ?
          ??ON? ?a.city? ?=? ?p.city? ?
          ??ORDER? ?BY? ?p.pub_name? ?ASC,? ?a.au_lname? ?ASC,? ?a.au_fname? ?ASC? ?
          ? ?
          ??下面是結果集:? ?
          ? ?
          ??au_fname? ?au_lname? ?pub_name? ???
          ??--------------------? ?------------------------------? ?-----------------? ???
          ??Reginald? ?Blotchet-Halls? ?NULL? ?
          ??Michel? ?DeFrance? ?NULL? ?
          ??Innes? ?del? ?Castillo? ?NULL? ?
          ??Ann? ?Dull? ?NULL? ?
          ??Marjorie? ?Green? ?NULL? ?
          ??Morningstar? ?Greene? ?NULL? ?
          ??Burt? ?Gringlesby? ?NULL? ?
          ??Sheryl? ?Hunter? ?NULL? ?
          ??Livia? ?Karsen? ?NULL? ?
          ??Charlene? ?Locksley? ?NULL? ?
          ??Stearns? ?MacFeather? ?NULL? ?
          ??Heather? ?McBadden? ?NULL? ?
          ??Michael? ?O'Leary? ?NULL? ?
          ??Sylvia? ?Panteley? ?NULL? ?
          ??Albert? ?Ringer? ?NULL? ?
          ??Anne? ?Ringer? ?NULL? ?
          ??Meander? ?Smith? ?NULL? ?
          ??Dean? ?Straight? ?NULL? ?
          ??Dirk? ?Stringer? ?NULL? ?
          ??Johnson? ?White? ?NULL? ?
          ??Akiko? ?Yokomoto? ?NULL? ?
          ??Abraham? ?Bennet? ?Algodata? ?Infosystems? ?
          ??Cheryl? ?Carson? ?Algodata? ?Infosystems? ?
          ? ?
          ??(23? ?row(s)? ?affected)? ?
          ? ?
          ??不管是否與? ?publishers? ?表中的? ?city? ?列匹配,LEFT? ?OUTER? ?JOIN? ?均會在結果中包含? ?authors? ?表的所有行。注意:結果中所列的大多數作者都沒有相匹配的數據,因此,這些行的? ?pub_name? ?列包含空值。? ?
          ? ?
          ??使用右向外聯接? ?
          ??假設在? ?city? ?列上聯接? ?authors? ?表和? ?publishers? ?表。結果只顯示在出版商所在城市居住的作者(本例中為? ?Abraham? ?Bennet? ?和? ?Cheryl? ?Carson)。SQL-92? ?右向外聯接運算符? ?RIGHT? ?OUTER? ?JOIN? ?指明:不管第一個表中是否有匹配的數據,結果將包含第二個表中的所有行。? ?
          ? ?
          ??若要在結果中包括所有的出版商,而不管城市中是否還有出版商居住,請使用? ?SQL-92? ?右向外聯接。下面是? ?Transact-SQL? ?右向外聯接的查詢和結果:? ?
          ? ?
          ??USE? ?pubs? ?
          ??SELECT? ?a.au_fname,? ?a.au_lname,? ?p.pub_name? ?
          ??FROM? ?authors? ?AS? ?a? ?RIGHT? ?OUTER? ?JOIN? ?publishers? ?AS? ?p? ?
          ??ON? ?a.city? ?=? ?p.city? ?
          ??ORDER? ?BY? ?p.pub_name? ?ASC,? ?a.au_lname? ?ASC,? ?a.au_fname? ?ASC? ?
          ? ?
          ??下面是結果集:? ?
          ? ?
          ??au_fname? ?au_lname? ?pub_name? ???
          ??--------------------? ?------------------------? ?--------------------? ???
          ??Abraham? ?Bennet? ?Algodata? ?Infosystems? ?
          ??Cheryl? ?Carson? ?Algodata? ?Infosystems? ?
          ??NULL? ?NULL? ?Binnet? ?&? ?Hardley? ?
          ??NULL? ?NULL? ?Five? ?Lakes? ?Publishing? ?
          ??NULL? ?NULL? ?GGG&G? ?
          ??NULL? ?NULL? ?Lucerne? ?Publishing? ?
          ??NULL? ?NULL? ?New? ?Moon? ?Books? ?
          ??NULL? ?NULL? ?Ramona? ?Publishers? ?
          ??NULL? ?NULL? ?Scootney? ?Books? ?
          ? ?
          ??(9? ?row(s)? ?affected)? ?
          ? ?
          ??使用謂詞(如將聯接與常量比較)可以進一步限制外聯接。下例包含相同的右向外聯接,但消除銷售量低于? ?50? ?本的書籍的書名:? ?
          ? ?
          ??USE? ?pubs? ?
          ??SELECT? ?s.stor_id,? ?s.qty,? ?t.title? ?
          ??FROM? ?sales? ?s? ?RIGHT? ?OUTER? ?JOIN? ?titles? ?t? ?
          ??ON? ?s.title_id? ?=? ?t.title_id? ?
          ??AND? ?s.qty? ?>? ?50? ?
          ??ORDER? ?BY? ?s.stor_id? ?ASC? ?
          ? ?
          ??下面是結果集:? ?
          ? ?
          ??stor_id? ?qty? ?title? ???
          ??-------? ?------? ?---------------------------------------------------------? ???
          ??(null)? ?(null)? ?But? ?Is? ?It? ?User? ?Friendly?? ???
          ??(null)? ?(null)? ?Computer? ?Phobic? ?AND? ?Non-Phobic? ?Individuals:? ?Behavior? ???
          ??Variations? ???
          ??(null)? ?(null)? ?Cooking? ?with? ?Computers:? ?Surreptitious? ?Balance? ?Sheets? ???
          ??(null)? ?(null)? ?Emotional? ?Security:? ?A? ?New? ?Algorithm? ???
          ??(null)? ?(null)? ?Fifty? ?Years? ?in? ?Buckingham? ?Palace? ?Kitchens? ???
          ??7066? ?75? ?Is? ?Anger? ?the? ?Enemy?? ???
          ??(null)? ?(null)? ?Life? ?Without? ?Fear? ???
          ??(null)? ?(null)? ?Net? ?Etiquette? ???
          ??(null)? ?(null)? ?Onions,? ?Leeks,? ?and? ?Garlic:? ?Cooking? ?Secrets? ?of? ?the? ???
          ??Mediterranean? ???
          ??(null)? ?(null)? ?Prolonged? ?Data? ?Deprivation:? ?Four? ?Case? ?Studies? ???
          ??(null)? ?(null)? ?Secrets? ?of? ?Silicon? ?Valley? ???
          ??(null)? ?(null)? ?Silicon? ?Valley? ?Gastronomic? ?Treats? ???
          ??(null)? ?(null)? ?Straight? ?Talk? ?About? ?Computers? ???
          ??(null)? ?(null)? ?Sushi,? ?Anyone?? ???
          ??(null)? ?(null)? ?The? ?Busy? ?Executive's? ?Database? ?Guide? ???
          ??(null)? ?(null)? ?The? ?Gourmet? ?Microwave? ???
          ??(null)? ?(null)? ?The? ?Psychology? ?of? ?Computer? ?Cooking? ???
          ??(null)? ?(null)? ?You? ?Can? ?Combat? ?Computer? ?Stress!? ???
          ? ?
          ??(18? ?row(s)? ?affected)? ?
          ? ?
          ??有關謂詞的更多信息,請參見? ?WHERE。? ???
          ? ?
          ??使用完整外部聯接? ?
          ??若要通過在聯接結果中包括不匹配的行保留不匹配信息,請使用完整外部聯接。Microsoft?? ?SQL? ?Server?? ?2000? ?提供完整外部聯接運算符? ?FULL? ?OUTER? ?JOIN,不管另一個表是否有匹配的值,此運算符都包括兩個表中的所有行。? ?
          ? ?
          ??假設在? ?city? ?列上聯接? ?authors? ?表和? ?publishers? ?表。結果只顯示在出版商所在城市居住的作者(本例中為? ?Abraham? ?Bennet? ?和? ?Cheryl? ?Carson)。SQL-92? ?FULL? ?OUTER? ?JOIN? ?運算符指明:不管表中是否有匹配的數據,結果將包括兩個表中的所有行。? ?
          ? ?
          ??若要在結果中包括所有作者和出版商,而不管城市中是否有出版商或者出版商是否住在同一個城市,請使用完整外部聯接。下面是? ?Transact-SQL? ?完整外部聯接的查詢和結果:? ?
          ? ?
          ??USE? ?pubs? ?
          ??SELECT? ?a.au_fname,? ?a.au_lname,? ?p.pub_name? ?
          ??FROM? ?authors? ?a? ?FULL? ?OUTER? ?JOIN? ?publishers? ?p? ?
          ??ON? ?a.city? ?=? ?p.city? ?
          ??ORDER? ?BY? ?p.pub_name? ?ASC,? ?a.au_lname? ?ASC,? ?a.au_fname? ?ASC? ?
          ? ?
          ??下面是結果集:? ?
          ? ?
          ??au_fname? ?au_lname? ?pub_name? ???
          ??--------------------? ?----------------------------? ?--------------------? ???
          ??Reginald? ?Blotchet-Halls? ?NULL? ?
          ??Michel? ?DeFrance? ?NULL? ?
          ??Innes? ?del? ?Castillo? ?NULL? ?
          ??Ann? ?Dull? ?NULL? ?
          ??Marjorie? ?Green? ?NULL? ?
          ??Morningstar? ?Greene? ?NULL? ?
          ??Burt? ?Gringlesby? ?NULL? ?
          ??Sheryl? ?Hunter? ?NULL? ?
          ??Livia? ?Karsen? ?NULL? ?
          ??Charlene? ?Locksley? ?NULL? ?
          ??Stearns? ?MacFeather? ?NULL? ?
          ??Heather? ?McBadden? ?NULL? ?
          ??Michael? ?O'Leary? ?NULL? ?
          ??Sylvia? ?Panteley? ?NULL? ?
          ??Albert? ?Ringer? ?NULL? ?
          ??Anne? ?Ringer? ?NULL? ?
          ??Meander? ?Smith? ?NULL? ?
          ??Dean? ?Straight? ?NULL? ?
          ??Dirk? ?Stringer? ?NULL? ?
          ??Johnson? ?White? ?NULL? ?
          ??Akiko? ?Yokomoto? ?NULL? ?
          ??Abraham? ?Bennet? ?Algodata? ?Infosystems? ?
          ??Cheryl? ?Carson? ?Algodata? ?Infosystems? ?
          ??NULL? ?NULL? ?Binnet? ?&? ?Hardley? ?
          ??NULL? ?NULL? ?Five? ?Lakes? ?Publishing? ?
          ??NULL? ?NULL? ?GGG&G? ?
          ??NULL? ?NULL? ?Lucerne? ?Publishing? ?
          ??NULL? ?NULL? ?New? ?Moon? ?Books? ?
          ??NULL? ?NULL? ?Ramona? ?Publishers? ?
          ??NULL? ?NULL? ?Scootney? ?Books
          聯接條件可在? ?FROM? ?或? ?WHERE? ?子句中指定,建議在? ?FROM? ?子句中指定聯接條件。WHERE? ?和? ?HAVING? ?子句也可以包含搜索條件,以進一步篩選聯接條件所選的行。? ?
          ? ?
          ??聯接可分為以下幾類:? ???
          ? ?
          ??內聯接(典型的聯接運算,使用像? ?=? ?或? ?<>? ?之類的比較運算符)。包括相等聯接和自然聯接。? ???
          ??內聯接使用比較運算符根據每個表共有的列的值匹配兩個表中的行。例如,檢索? ?students? ?和? ?courses? ?表中學生標識號相同的所有行。? ?
          ? ?
          ??外聯接。外聯接可以是左向外聯接、右向外聯接或完整外部聯接。? ???
          ??在? ?FROM? ?子句中指定外聯接時,可以由下列幾組關鍵字中的一組指定:? ?
          ? ?
          ??LEFT? ?JOIN? ?或? ?LEFT? ?OUTER? ?JOIN。? ???
          ??左向外聯接的結果集包括? ?LEFT? ?OUTER? ?子句中指定的左表的所有行,而不僅僅是聯接列所匹配的行。如果左表的某行在右表中沒有匹配行,則在相關聯的結果集行中右表的所有選擇列表列均為空值。? ?
          ? ?
          ??RIGHT? ?JOIN? ?或? ?RIGHT? ?OUTER? ?JOIN。? ???
          ??右向外聯接是左向外聯接的反向聯接。將返回右表的所有行。如果右表的某行在左表中沒有匹配行,則將為左表返回空值。? ?
          ? ?
          ??FULL? ?JOIN? ?或? ?FULL? ?OUTER? ?JOIN。? ???
          ??完整外部聯接返回左表和右表中的所有行。當某行在另一個表中沒有匹配行時,則另一個表的選擇列表列包含空值。如果表之間有匹配行,則整個結果集行包含基表的數據值。? ?
          ? ?
          ??交叉聯接。? ???
          ??交叉聯接返回左表中的所有行,左表中的每一行與右表中的所有行組合。交叉聯接也稱作笛卡爾積。? ?
          ? ?
          ??例如,下面的內聯接檢索與某個出版商居住在相同州和城市的作者:? ?
          ? ?
          ??USE? ?pubs? ?
          ??SELECT? ?a.au_fname,? ?a.au_lname,? ?p.pub_name? ?
          ??FROM? ?authors? ?AS? ?a? ?INNER? ?JOIN? ?publishers? ?AS? ?p? ?
          ? ?? ???ON? ?a.city? ?=? ?p.city? ?
          ? ?? ???AND? ?a.state? ?=? ?p.state? ?
          ??ORDER? ?BY? ?a.au_lname? ?ASC,? ?a.au_fname? ?ASC? ?
          ? ?
          ??FROM? ?子句中的表或視圖可通過內聯接或完整外部聯接按任意順序指定;但是,用左或右向外聯接指定表或視圖時,表或視圖的順序很重要。有關使用左或右向外聯接排列表的更多信息,請參見使用外聯接。? ???
          ? ?
          ? ?
          ? ?
          ? ?
          ??例子:? ?
          ??a表? ???id? ?name? ???b表? ???id? ?job? ?parent_id? ?
          ? ?? ?? ?? ???1? ?張3? ?? ?? ?? ?? ?? ? 1? ???23? ???1? ?
          ? ?? ?? ?? ???2? ?李四? ?? ?? ?? ?? ???2? ???34? ???2? ?
          ? ?? ?? ?? ???3? ?王武? ?? ?? ?? ?? ???3? ???34? ???4? ?
          ? ?
          ??a.id同parent_id? ?存在關系? ?
          ? ?
          ??內連接? ?
          ??select? ?a.*,b.*? ?from? ?a? ?inner? ?join? ?b? ???on? ?a.id=b.parent_id? ?
          ? ?
          ??結果是? ???
          ??1? ?張3? ?? ?? ?? ?? ?? ? 1? ???23? ???1? ?
          ??2? ?李四? ?? ?? ?? ?? ???2? ???34? ???2? ?
          ? ?
          ??左連接? ?
          ? ?
          ??select? ?a.*,b.*? ?from? ?a? ?left? ?join? ?b? ???on? ?a.id=b.parent_id? ?
          ? ?
          ??結果是? ???
          ??1? ?張3? ?? ?? ?? ?? ?? ? 1? ???23? ???1? ?
          ??2? ?李四? ?? ?? ?? ?? ???2? ???34? ???2? ?
          ??3? ?王武? ?? ?? ?? ?? ???null? ?
          ??右連接? ?
          ??select? ?a.*,b.*? ?from? ?a? ?right? ?join? ?b? ???on? ?a.id=b.parent_id? ?
          ? ?
          ??結果是? ???
          ??1? ?張3? ?? ?? ?? ?? ?? ? 1? ???23? ???1? ?
          ??2? ?李四? ?? ?? ?? ?? ???2? ???34? ???2? ?
          ??null? ?? ?? ?? ?? ???3? ???34? ???4? ?
          ? ?
          ??完全連接? ?
          ? ?
          ??select? ?a.*,b.*? ?from? ?a? ?full? ?join? ?b? ???on? ?a.id=b.parent_id? ?
          ? ?
          ? ?
          ??結果是? ???
          ??1? ?張3? ?? ?? ?? ?? ?? ? 1? ???23? ???1? ?
          ??2? ?李四? ?? ?? ?? ?? ???2? ???34? ???2? ?
          ??null? ?? ?? ?? ?? ???3? ???34? ???4? ?
          ??3? ?王武? ?? ?? ?? ?? ???null
          左連接例子

          select count(*) as title from seek_user t1 left join sekk_info t2 on t1.seek_id=t2.user_id where SeekusType!='8'


          posted on 2008-04-13 17:04 金家寶 閱讀(1066) 評論(0)  編輯  收藏 所屬分類: Mysql

          主站蜘蛛池模板: 余庆县| 阳信县| 博乐市| 突泉县| 比如县| 三河市| 潜江市| 响水县| 辉县市| 洛南县| 墨脱县| 皋兰县| 麻栗坡县| 银川市| 麟游县| 微博| 洪雅县| 邵阳市| 奉贤区| 通辽市| 会宁县| 渭源县| 班戈县| 伊川县| 云林县| 广丰县| 宝坻区| 耒阳市| 西盟| 高邮市| 海原县| 友谊县| 五指山市| 明星| 东明县| 新泰市| 城市| 石柱| 宁河县| 博乐市| 五家渠市|