当前位置:首页 » 多媒体相关

在线等待,数据表联接问题


我现在有a,b,c,d   4张数据表,我想取出表c里面字段cc为2的记录的id,取出表d里面字段dd为5的id,然后根据表c,d中取出的相同的id号之后,到表b里面在取出这些id好,最后根据这些id号到表a里面取出这些id号的记录,请问sql语句怎么写?

推荐阅读

  • 3G大肚量三星智能机i308暴跌400元成都 [详细内容]
  • 又有低价彩屏MP3 六万色256M价格触底 [详细内容]
  • 快速在电话本找到所需的电话 [详细内容]
  • 智能新感觉 奔迈Treo 680到货4799成都 [详细内容]
  • 视频是强项 高科真彩MP3 256M仅4×× [详细内容]
  • 让背景灯光延长的小方法 [详细内容]
  • 高端商务机 多普达C858降至5880元成都 [详细内容]
  • 网友回答:
    网友:dlkfth

    select   a.*   from   a   inner   join   b   on   a.id=b.id  
      inner   join   c   on   c.id=a.id   inner   join   d  
      on   a.id=d.id   where   d.dd=5   and   c.cc=2

    网友:crazyfor

    select   *   from   a   where   id   in(select   c.id   from   c   inner   join   d   on   c.id=d.id   where   c.cc=2   )  
       
      --------------------  
      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   表。结果只显示在出版商所在城市居住的作者。  
       
      若要在结果中包括所有的作者,而不管出版商是否住在同一个城市,请使用   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   oleary   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   表。结果只显示在出版商所在城市居住的作者。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   executives   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   表。结果只显示在出版商所在城市居住的作者。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   oleary   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)    
     

    网友:happy_0325

    select   *   from   a   where   id   in(select   c.id   from   c,d   where   c.id=d.id   and   c.cc=2   and   d.dd=5)

    网友:txlicenhe

    楼主在带大家绕圈子。^_^  
      select   id   from   a  
      join   b   on   a.id   =   b.id    
      join   c   on   b.id   =   c.id  
      join   d   on   c.id   =   d.id  
      where   c.cc   =   2   and   d.dd   =   5

    网友:hjb111

    select   a.*   from   a   where   a.id   in   (select   b.id   from   b,c,d   where   b.id=c.id   and   c.id=d.id   and   c.dd=2   and   d.dd=5)

    网友:pengdali

     
      select   id   from   a   where   id   in   (select   id   from   b   where   id   in   (   select   id   from   c   where   cc=2   and   id   in   (select   id   from   d   where   dd=5)))  
       
      或:  
       
      select   a.*   from   c   join   d   on   c.cc=2   and   d.dd=5   and   c.id=d.id   join   b   on   c.id=b.id   join   a   on   b.id=a.id

    .

    讨论区

    Login