5-8万年薪顶级嵌入式,京沪深就业地 浅谈并行编程中的任务分解模式
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

【菜鸟MM提问!何为inner join、left join、right join ???】

楼主shuiyueer19([水月儿])2005-12-29 09:26:16 在 MS-SQL Server / 基础类 提问

昨天在我的一个问题中  
   
  发现红尘GG和钻石王老五GG都用到了inner   join和left   join      
   
  顾名思义     inner是内部的意思  
                      join   是连接的意思吧    
                      合起来就是内部连接的意思么??  
   
  啥叫内连接啊??       肯定还有外部连接吧?        
   
  内、外有什么联系和区别么?  
   
  左、右连接又是怎么回事??       晕死呀~~~  
   
  那我只会用的这个     select   *   from     table1,table2     又叫什么连接呢?  
   
  请红尘GG和钻石王老五GG不吝指教  
   
  刚学SQL一段时间     基本上啥也不会  
   
  &-_-& 问题点数:100、回复次数:21Top

1 楼shuiyueer19([水月儿])回复于 2005-12-29 09:28:57 得分 0

请尽量解释的通俗易懂一点好吗??  
   
  或者给个SQL实例语句看看?  
   
  偶最烦看书了  
   
  一堆的理论  
   
  看了也是白看  
   
  I   will   wait   for   U     online~~~Top

2 楼whbo(王红波(年轻人,要有所作为))回复于 2005-12-29 09:32:06 得分 15

使用外联接  
  仅当至少有一个同属于两表的行符合联接条件时,内联接才返回行。内联接消除与另一个表中的任何行不匹配的行。而外联接会返回   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)  
  Top

3 楼whbo(王红波(年轻人,要有所作为))回复于 2005-12-29 09:32:12 得分 0

 
  不管是否与   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  
   
  (30   row(s)   affected)  
   
  Top

4 楼whbo(王红波(年轻人,要有所作为))回复于 2005-12-29 09:32:51 得分 0

左连接,以左为主,右连接,以右为主了Top

5 楼happydreamer(www.sz.js.cn,www.gyxk.com)回复于 2005-12-29 09:37:43 得分 15

declare   @a   table(a   int,b   int)  
  declare   @b   table(a   int,b   int)  
  insert   @a   values(1,1)  
  insert   @a   values(2,2)  
  insert   @b   values(1,1)  
  insert   @b   values(3,3)  
   
  --左:  
  select   *   from   @a   Aa   left   join   @b   Bb   on   Aa.a=Bb.a  
  --右:  
  select   *   from   @a   Aa   right   join   @b   Bb   on   Aa.a=Bb.a  
  --内  
  select   *   from   @a   Aa   join   @b   Bb   on   Aa.a=Bb.a  
  --外:  
  select   *   from   @a   Aa   full   join   @b   Bb   on   Aa.a=Bb.a  
  --完全  
  select   *   from   @a,@b  
   
   
  cross   join   是笛卡儿乘积   就是一张表的行数乘以另一张表的行数  
  left     join   第一张表的连接列在第二张表中没有匹配是,第二张表中的值返回null  
  right   join   第二张表的连接列在第一张表中没有匹配是,第一张表中的值返回null    
  full     join   返回两张表中的行   left   join+right   join  
  inner   join   只返回两张表连接列的匹配项  
  Top

6 楼lsqkeke(可可)回复于 2005-12-29 09:38:24 得分 5

哈哈   学习Top

7 楼lsqkeke(可可)回复于 2005-12-29 09:39:59 得分 0

happydreamer(小黑)     说得简单易懂!  
    好的   :)Top

8 楼shuiyueer19([水月儿])回复于 2005-12-29 09:40:40 得分 0

哇~~  
   
  怎么这么复杂??  
   
  我还有个疑问  
   
  昨天红尘GG和钻石邹老大GG的SQL语句  
   
  select   *   from   t1   inner   join   t2     on   ....  
  left   join   t2   on   ....  
  left   join   t3   on   ....  
   
  可否用这种方式呢?  
   
  select   *   from   t1,t2,t3   where   ....  
   
  咦~~     把上面的where换成on   可以不呀?Top

9 楼shuiyueer19([水月儿])回复于 2005-12-29 09:44:24 得分 0

小黑GG说解释得还比较简洁易懂  
   
  我试试小黑GG的SQL语句体验一下  
   
  顺便说一句  
   
  我家的小狗狗就叫“小黑”  
   
  嘿嘿  
   
  别生气噢  
   
  &-_-&Top

10 楼liujx_1999(Fly)回复于 2005-12-29 09:47:53 得分 15

if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N'[dbo].[T_1]')   and   OBJECTPROPERTY(id,   N'IsUserTable')   =   1)  
  drop   table   [dbo].[T_1]  
  CREATE   TABLE   [dbo].[T_1]   (  
  [F_Mst]   [decimal](10,   0)   NOT   NULL   ,  
  [f1]   [varchar]   (50)     NULL    
  )  
  GO  
  if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N'[dbo].[T_2]')   and   OBJECTPROPERTY(id,   N'IsUserTable')   =   1)  
  drop   table   [dbo].[T_2]  
   
  CREATE   TABLE   [dbo].[T_2]   (  
  [F_Mst_T]   [int]   NOT   NULL   ,  
  [f1]   [varchar]   (50)   COLLATE   Chinese_Taiwan_Stroke_CI_AS   NULL    
  )    
  GO  
  insert   into   T_1(F_Mst,f1)  
  select   '1','aa'  
  union   select   '2','bb'  
  union   select   '3','cc'  
  union   select   '4','aa'  
  union   select   '5','ee'  
  union   select   '6','ff'  
  insert   into   T_2(F_Mst_T,f1)  
  select   '1','gg'  
  union   select   '2','kk'  
   
  select   *   from   T_1   a     left     join   T_2     b   on   a.F_Mst=b.F_Mst_T  
  select   *   from   T_1   a     right     join   T_2     b   on   a.F_Mst=b.F_Mst_T  
  select   *   from   T_1   a     inner     join   T_2     b   on   a.F_Mst=b.F_Mst_T  
  select   *   from   T_2   a     inner     join   T_1     b   on   b.F_Mst=A.F_Mst_T  
  慢慢體會Top

11 楼shuiyueer19([水月儿])回复于 2005-12-29 09:48:26 得分 0

不是吧  
   
  这打击偶对SQL数据库的积极性吗?  
   
  菜鸟也是鸟啊  
   
  晕死~~Top

12 楼lw1a2(一刀 现在改六点下班了:()回复于 2005-12-29 09:51:31 得分 5

标明性别的帖子....Top

13 楼zlp321002(Life Is Good,Let's Shine)回复于 2005-12-29 09:54:21 得分 20

查询分析器中执行:  
  --建表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,table2Top

14 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2005-12-29 10:47:25 得分 20

select   *   from   t1   inner   join   t2     on   ....  
  left   join   t2   on   ....  
  left   join   t3   on   ....  
   
  可否用这种方式呢?  
   
  select   *   from   t1,t2,t3   where   ....  
   
  咦~~     把上面的where换成on   可以不呀?  
  -----------------------------------------------------------------------------------------------------  
  可以,不过where   条件的书写需要注意:  
  t1   inner   join   t2   on   t1.id=t2.id  
  ==>  
  t1.id=t2.id  
   
  t1   left   join   t2   on   t1.id=t2.id  
  ==>  
  t1.id=*t2.id  
   
  t1   right   join   t2   on   t1.id=t2.id  
  ==>  
  t1.id*=t2.idTop

15 楼shuiyueer19([水月儿])回复于 2005-12-29 10:48:19 得分 0

还有么?Top

16 楼shuiyueer19([水月儿])回复于 2005-12-29 10:50:38 得分 0

感谢  
   
  whbo(王红波(年轻人,要有所作为))    
   
  happydreamer(小黑)    
   
  lsqkeke(可可)    
   
  liujx_1999(Fly)  
   
    lw1a2(一刀   Blog:http://spaces.msn.com/members/lw1a2/)  
   
    zlp321002(她是我的唯一.)    
   
    libin_ftsafe(子陌红尘)    
   
  晕     红尘GG总是在最后关头才出现Top

17 楼shuiyueer19([水月儿])回复于 2005-12-29 10:52:31 得分 0

谢谢以上GG的不吝指点  
   
  我会把你们的答案打印出来  
   
  晚上带回家慢慢研究学习  
   
  谢谢~~  
   
  Thanks   again     ~~~~Top

18 楼bbsadsql(刘金和)回复于 2005-12-29 11:39:22 得分 5

zlp321002(她是我的唯一.)   (   )   信誉:104    
  给我上了一堂很漂亮的课.  
  非常感谢Top

19 楼ocan(OK! I Can!!!)回复于 2005-12-30 13:17:15 得分 0

参看  
  http://www.gamvan.com/database/mssql/2005/10/2005101818593073499.html  
  Top

20 楼roxette909()回复于 2006-01-23 15:53:40 得分 0

markTop

21 楼easyboot(易宝)回复于 2006-03-09 09:15:31 得分 0

不错   收藏Top

相关问题

  • 帮MM提问
  • 提问提问...
  • 关于inner join?
  • UPDATE PaymentInfo INNER JOIN
  • inner join 查询
  • 【!菜鸟MM又来提问啦!简单的日期问题!】
  • 重大发现:以“小女子”开头提问的是lovemeyy,以“漂亮mm”开头提问的是 stella2002
  • 提问
  • 提问!!
  • 提问!

关键词

  • 联接
  • 语句
  • 连接
  • 表
  • join
  • 子句
  • au
  • 结果
  • outer
  • 运算符

得分解答快速导航

  • 帖主:shuiyueer19
  • whbo
  • happydreamer
  • lsqkeke
  • liujx_1999
  • lw1a2
  • zlp321002
  • libin_ftsafe
  • bbsadsql

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
x 提问