CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

求SQL语句,最好一句搞定,有点小麻烦

楼主immc1979(毛毛虫)2006-03-13 15:24:43 在 MS-SQL Server / 基础类 提问

有表内容如下  
  id(key)       Pid(int)         class(int)         info(varchar(10))  
  1                   1                       1                           xx  
  2                   2                       1                           14  
  3                   3                       1                           aa  
  4                   4                       1                           16  
  5                   1                       2                           1f  
  6                   2                       2                           15  
  7                   3                       2                           ss  
  8                   4                       2                           de  
   
  想通过一个SQL语句吧上表列成如下格式输出  
      Pid                 class                 info1           info2  
      1                       1                           xx             1f  
      2                       1                           14             15  
      3                       1                           aa             ss  
      4                       1                           16             de  
   
  就是把class=2的info的内容按照pid添加到新字段info2中  
  问题点数:50、回复次数:22Top

1 楼zbxubing(冰)回复于 2006-03-13 15:34:20 得分 0

使用简单   UNION  
  下例中的结果集包括   Customers   和   SouthAmericanCustomers   这两个表的   ContactName、CompanyName、City   和   Phone   列的内容。  
   
  USE   Northwind  
  GO  
  IF   EXISTS(SELECT   TABLE_NAME   FROM   INFORMATION_SCHEMA.TABLES  
              WHERE   TABLE_NAME   =   'SouthAmericanCustomers')  
        DROP   TABLE   SouthAmericanCustomers  
  GO  
  --   Create   SouthAmericanCustomers   table.  
  SELECT   ContactName,   CompanyName,   City,   Phone  
  INTO   SouthAmericanCustomers  
  FROM   Customers  
  WHERE   Country   IN   ('USA',   'Canada')  
  GO  
  --   Here   is   the   simple   union.  
  USE   Northwind  
  SELECT   ContactName,   CompanyName,   City,   Phone  
  FROM   Customers  
  WHERE   Country   IN   ('USA',   'Canada')  
  UNION  
  SELECT   ContactName,   CompanyName,   City,   Phone  
  FROM   SouthAmericanCustomers  
  ORDER   BY   CompanyName,   ContactName   ASC  
  GOTop

2 楼zbxubing(冰)回复于 2006-03-13 15:36:04 得分 0

我开始理解错了,是个连接,通过pid  
  使用内联接  
  内联接是用比较运算符比较要联接列的值的联接。  
   
  在   SQL-92   标准中,内联接可在   FROM   或   WHERE   子句中指定。这是   WHERE   子句中唯一一种   SQL-92   支持的联接类型。WHERE   子句中指定的内联接称为旧式内联接。  
   
  下面的   Transact-SQL   查询是内联接的一个示例:  
   
  USE   pubs  
  SELECT   *  
  FROM   authors   AS   a   INNER   JOIN   publishers   AS   p  
        ON   a.city   =   p.city  
  ORDER   BY   a.au_lname   DESC  
   
  此内联接称为相等联接。它返回两个表中的所有列,但只返回在联接列中具有相等值的行。  
   
  下面是结果集:  
   
  au_id                 au_lname     au_fname   phone                   address                     city          
  -----------     --------     --------   ------------     ---------------     --------  
  238-95-7766     Carson         Cheryl       415   548-7723     589   Darwin   Ln.         Berkeley  
  409-56-7008     Bennet         Abraham     415   658-9932     6223   Bateman   St.     Berkeley  
   
  state   zip       contract   pub_id   pub_name                             city           state   country  
  -----   -----   --------   ------   ---------------------   --------   -----   -------  
  CA         94705   1                 1389       Algodata   Infosystems     Berkeley   CA         USA          
  CA         94705   1                 1389       Algodata   Infosystems     Berkeley   CA         USA          
   
  (2   row(s)   affected)  
   
  在结果集中,city   列出现两次。由于重复相同的信息没有意义,因此可以通过更改选择列表消除两个相同列中的一个。其结果称为自然联接。可以重新表述前面的   Transact-SQL   查询以形成自然联接。例如:  
   
  USE   pubs  
  SELECT   p.pub_id,   p.pub_name,   p.state,   a.*  
  FROM   publishers   p   INNER   JOIN   authors   a  
        ON   p.city   =   a.city  
  ORDER   BY   a.au_lname   ASC,   a.au_fname   ASC  
   
  下面是结果集:  
   
  pub_id   pub_name                             state         au_id                 au_lname     au_fname  
  ------   ---------------               --------   -----------     --------     --------   1389       Algodata   Infosystems     CA               409-56-7008     Bennet         Abraham  
  1389       Algodata   Infosystems     CA               238-95-7766     Carson         Cheryl  
   
  phone                   address                     city             state   zip       contract  
  ---------------     -------------   --------     -----   -----   ---------  
  415   658-9932     6223   Bateman   St.   Berkeley     CA         94705   1  
  415   548-7723     589   Darwin   Ln.       Berkeley     CA         94705   1  
   
  (2   row(s)   affected)  
   
  本示例中,publishers.city   没有出现在结果中。  
   
  使用等号以外的运算符的联接  
  也可以联接两个不相等的列中的值。用于内联接的运算符和谓词同样也可用于不相等联接。有关联接中可用的运算符和谓词的更多信息,请参见在表达式中使用运算符和   WHERE。    
   
  下面的   Transact-SQL   示例是一个大于   (>)   联接,可用于查找住在   Massachusetts   之后(按字母顺序排列)的州的   New   Moon   作家,Massachusetts   是   New   Moon   Books   的所在地。  
   
  USE   pubs  
  SELECT   p.pub_name,   p.state,   a.au_lname,   a.au_fname,   a.state  
  FROM   publishers   p   INNER   JOIN   authors   a  
        ON   a.state   >   p.state  
  WHERE   p.pub_name   =   'New   Moon   Books'  
  ORDER   BY   au_lname   ASC,   au_fname   ASC  
   
  下面是结果集:  
   
  pub_name                   state       au_lname                           au_fname                           state    
  ----------------   -------   --------------------   --------------------   -----    
  New   Moon   Books       MA         Blotchet-Halls                   Reginald                           OR  
  New   Moon   Books       MA         del   Castillo                       Innes                                 MI  
  New   Moon   Books       MA         Greene                                   Morningstar                     TN  
  New   Moon   Books       MA         Panteley                               Sylvia                               MD  
  New   Moon   Books       MA         Ringer                                   Albert                               UT  
  New   Moon   Books       MA         Ringer                                   Anne                                   UT  
   
  (6   row(s)   affected)  
   
  使用不等运算符的联接  
  很少使用不等联接   (<   >)。通常不等联接只有与自联接同时使用才有意义。例如,可以使用下面的不等   Transact-SQL   联接和自联接查找包含不同价格的两本或多本廉价(低于   $15)书的类别:  
   
  USE   pubs  
  SELECT   DISTINCT   t1.type,   t1.price  
  FROM   titles   t1   INNER   JOIN   titles   t2    
        ON   t1.type   =   t2.type  
        AND   t1.price   <>   t2.price  
  WHERE   t1.price   <   $15   AND   t2.price   <   $15  
   
   
   
  说明     表达式   NOT   column_name   =   column_name   与表达式   column_name   <   >   column_name   等效。  
   
   
  下面的   Transact-SQL   示例中,使用不等联接和自联接的组合查找   titleauthor   表中的所有行,在该表中有两行或多行具有相同的   title_id   但   au_id   号不同(即一本书有多个作者):  
   
  USE   pubs  
  SELECT   DISTINCT   t1.au_id,   t1.title_id  
  FROM   titleauthor   t1   INNER   JOIN   titleauthor   t2    
        ON   t1.title_id   =   t2.title_id  
  WHERE   t1.au_id   <>   t2.au_id  
  ORDER   BY   t1.au_id  
   
  下面是结果集:  
   
  au_id                         title_id  
  -----------                   --------  
  213-46-8915                   BU1032  
  267-41-2394                   BU1111  
  267-41-2394                   TC7777  
  409-56-7008                   BU1032  
  427-17-2319                   PC8888  
  472-27-2349                   TC7777  
  672-71-3249                   TC7777  
  722-51-5454                   MC3021  
  724-80-9391                   BU1111  
  724-80-9391                   PS1372  
  756-30-7391                   PS1372  
  846-92-7186                   PC8888  
  899-46-2035                   MC3021  
  899-46-2035                   PS2091  
  998-72-3567                   PS2091  
   
  (15   row(s)   affected)Top

3 楼zbxubing(冰)回复于 2006-03-13 15:38:39 得分 5

(select   *   from   t   where   class=1)看作一个表  
  (select   *   from   t   where   class=2)看作另外一个表  
  select   *   from   (select   *   from   t   where   class=1)   t1   inner   join   (select   *   from   t   where   class=2)   t2   on   t1.id=t2.pidTop

4 楼yuweiwei(YWW(杨思))回复于 2006-03-13 15:44:53 得分 0

 
  select   表.pid,表.class,表.info,tt.info   as   info2      
   
  from   表   ,   (select   pid,info   from   表   where   class='2')tt  
  where   表.pid=tt.pid    
     
  Top

5 楼immc1979(毛毛虫)回复于 2006-03-13 15:50:02 得分 0

好像不行啊,输出还是两个表结果,没有整到一个表中间去啊Top

6 楼yuweiwei(YWW(杨思))回复于 2006-03-13 15:50:38 得分 0

对不起,我的错了,应该是楼上的才对!!!Top

7 楼immc1979(毛毛虫)回复于 2006-03-13 15:53:10 得分 0

好像约束条件有点不对,什么都不显示Top

8 楼yuweiwei(YWW(杨思))回复于 2006-03-13 15:54:05 得分 0

 
  select   AA.pid,AA.class,AA.info,tt.info   as   info2      
  from   (select   *   from   t   where   class=1)as   AA   ,    
  (select   pid,info   from   表   where   class='2')as   tt  
  where   AA.pid=tt.pid    
   
  应该这样才对  
     
   
       
  Top

9 楼happyflystone(无枪的狙击手)回复于 2006-03-13 15:55:54 得分 0

select   表.pid,表.class,表.info,tt.info   as   info2      
   
  from   表   ,   (select   pid,info   from   表   where   class='2')tt  
  where   表.pid=tt.pid    
  Top

10 楼immc1979(毛毛虫)回复于 2006-03-13 16:01:08 得分 0

仍然不行……  
  小有一点郁闷了Top

11 楼huailairen(流浪猫--很想养只猫,带着它到处流浪。)回复于 2006-03-13 16:01:59 得分 0

select     b.id,b.class,b.info   as   info1,a.info   as   info2  
  from   (select   *   from   tb   where   class=2)   a,(select   *   from   tb   where   class<>2)   b  
  where   a.pid=b.pidTop

12 楼yuweiwei(YWW(杨思))回复于 2006-03-13 16:02:25 得分 45

楼上的好象和我犯了同一个错误,应该在加上!  
   
      select   *   from   (  
  select   表.pid,表.class,表.info,tt.info   as   info2      
  from   表   ,   (select   pid,info   from   表   where   class='2')tt  
  where   表.pid=tt.pid    
  )as   BB   where   BB.class=1Top

13 楼immc1979(毛毛虫)回复于 2006-03-13 16:10:37 得分 0

楼上yuweiwei(YWW(杨思))貌似正解,我再领悟下,呵呵:)  
   
   
  -----------------------------------------------------  
  select   *   from   (  
  select   表.pid,表.class,表.info,tt.info   as   info2      
  from   表   ,   (select   pid,info   from   表   where   class='2')tt  
  where   表.pid=tt.pid    
  )as   tt   where   tt.class=1Top

14 楼zjdyzwx(十一月猪)回复于 2006-03-13 16:53:51 得分 0

CREATE   TABLE   #TEMP  
  (ID   INT   PRIMARY   KEY,   PID   INT,CLASS   INT   ,   INFO   VARCHAR(10))  
   
  INSERT   INTO   #TEMP  
  SELECT   1,                   1,                       1,                           'xx'   UNION   ALL  
  SELECT   2,                   2,                       1,                           '14'   UNION   ALL  
  SELECT   3,                   3,                       1,                           'aa'   UNION   ALL  
  SELECT   4,                   4,                       1,                           '16'   UNION   ALL  
  SELECT   5,                   1,                       2,                           '1f'   UNION   ALL  
  SELECT   6,                   2,                       2,                           '15'   UNION   ALL  
  SELECT   7,                   3,                       2,                           'ss'   UNION   ALL  
  SELECT   8,                   4,                       2,                           'de'  
   
     
   
  DECLARE   @S   VARCHAR(8000)  
  SET   @S   =   ''  
   
  SELECT   @S   =   @S   +   ','   +   'MAX(CASE   CLASS     WHEN   '   +   CAST(CLASS   AS   VARCHAR)   +'   THEN   INFO   END)   AS   INFO'   +   CAST(CLASS   AS   VARCHAR)    
  FROM   #TEMP    
  GROUP   BY   CLASS  
   
  EXEC('SELECT   PID,MIN(CLASS)   AS   ''CLASS''     '   +@S   +   '   FROM       #TEMP     GROUP   BY   PID   ')  
  DROP   TABLE   #TEMP  
  PRINT   @STop

15 楼tianyan316(与狼共舞,舞者岂是羊)回复于 2006-03-13 17:29:48 得分 0

select   temp3.pid,temp3.class,temp3.info   as   info1,tem.info   as   info2   from   temp3,(select   pid,info   from   temp3   where   class='2')tem   where    
  temp3.pid=tem.pid     and   temp3.class='1'Top

16 楼sjs9111(雉水寒)回复于 2006-03-14 11:47:38 得分 0

select    
          t.pid,  
          t.class,  
          t.info   info1,  
          g.info   info2  
  from  
          (select   *   from   t1   where   class=1)   t   ,  
          (select   class,info,pid   from   t1   where   class=2)   g  
  where  
          g.pid=t.pidTop

17 楼chpbing(chpbing)回复于 2006-03-14 12:10:51 得分 0

select   pid,  
                class,  
                info1   =   info,  
                info2   =   (select   info   from   表名  
                where   class=   2   and   pid   =   a.id)  
  from   表名   a  
  where   class   =   1Top

18 楼flysky_lfx(【今夜酷寒,不宜私奔】→暂时没有信息)回复于 2006-03-14 12:58:07 得分 0

 
   
  select   A.pid,A.class,A.info   as   info1   ,(select   B.info   from   表名     B   where   class=2   and   B.info=A.info   as   info2   )   from   表名     ATop

19 楼tengteng_rock(没完没了)回复于 2006-03-14 14:53:51 得分 0

select   a.id,a.pid,a.class,a.info,b.info    
  from   t,   (select   id,pid,class,info,info   from   t   where   class=2)   b    
  where   a.class   =   1   and   a.pid   =   b.pidTop

20 楼tengteng_rock(没完没了)回复于 2006-03-14 14:59:42 得分 0

刚才写错了:)  
  正解:  
   
  t为原表  
   
  select   t.id,t.pid,t.class,t.info,b.info    
  from   t,   (select   id,pid,class,info   from   t   where   class=2)   b    
  where   t.class   =   1   and   t.pid   =   b.pidTop

21 楼pengzhidong(东东)回复于 2006-03-14 17:06:18 得分 0

select   t1.Pid   as   Pid,   t1.class   as   class   ,t1.info   as   info1,t2.info   as   info2  
  from   tb   t1,   tb   t2  
  where   t1.class=1   and   t1.pid=t2.pid  
  不知道可不可行,大家试试!Top

22 楼pengzhidong(东东)回复于 2006-03-14 17:07:23 得分 0

select   t1.Pid   as   Pid,   t1.class   as   class   ,t1.info   as   info1,t2.info   as   info2  
  from   tb   t1,   tb   t2  
  where   t1.class=1   and   t2.class   <>1   and   t1.pid=t2.pid  
  不知道可不可行,大家试试!  
  Top

相关问题

  • sql 语句能不能搞定?
  • 一条sql语句如何搞定?
  • 求助SQL语句问题,搞定立即给分
  • 难以搞定的sql语句!请帮忙一起讨论!
  • 一个比较麻烦的问题,哪位能够一个SQL语句搞定?
  • 这个sql怎么写啊!!!??? 还有点小问题啊!!
  • 求SQL语句
  • sql语句。
  • sql语句?
  • sql 语句?

关键词

  • mp3
  • ps2
  • 联接
  • au
  • pid
  • 运算符
  • 表
  • 示例
  • info
  • lname

得分解答快速导航

  • 帖主:immc1979
  • zbxubing
  • yuweiwei

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo