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

求一sql,先谢谢了

楼主mousefog(IT老鼠)2006-03-03 16:47:01 在 MS-SQL Server / 基础类 提问

id                         name                           parentid  
                    2000                     菜系                           null  
                    2001                     省                               null  
                    2002                     车位                           null  
                    2003                     人均消费                   null  
                    2004                     市                               2001  
                    2005                     县                               2004  
  现在要执行一sql,得到如下结果  
                    id                         name                           parentid  
                    2000                     菜系                           null  
                    2001                     省                               null  
                    2004                     市                               2001  
                    2005                     县                               2004  
                    2002                     车位                           null  
                    2003                     人均消费                   null  
   
   
   
  问题点数:100、回复次数:9Top

1 楼wgsasd311(自强不息)回复于 2006-03-03 16:54:28 得分 0

select    
  *  
  from   tb   order   by   case   when   name='菜系'   then   0    
  when   name='省'   then   1   else   2   end,case   when   parentid   is   nul   then   9999  
  else   parentid   endTop

2 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2006-03-03 17:00:28 得分 80

在SQL   Server   2000里一个SQL恐怕写不出来,借助用户定义函数的实现方式:  
  --------------------------------------------------------------------------------------------------------------------------------  
  create   table   t   (id   int,name   varchar(10),parentid   int)  
  insert   into   t   select   2000,'菜系         ',null  
  insert   into   t   select   2001,'省             ',null  
  insert   into   t   select   2002,'车位         ',null  
  insert   into   t   select   2003,'人均消费',null  
  insert   into   t   select   2004,'市             ',2001  
  insert   into   t   select   2005,'县             ',2004  
  go  
   
   
  create   function   f_str(@id   int)  
  returns   varchar(1000)  
  as  
  begin  
          declare   @ret   varchar(1000),@pid   int  
          select   @pid=parentid,@ret=right('0000'+rtrim(id),5)   from   t   where   id=@id  
          while   @pid   is   not   null  
          begin  
                  set   @id=@pid  
                  select   @pid=parentid,@ret=right('0000'+rtrim(id),5)+@ret   from   t   where   id=@id  
          end  
          return   @ret  
  end  
  go  
   
   
  select   *   from   t   order   by   dbo.f_str(id)  
  go  
   
  /*  
  id                     name               parentid          
  -----------   ----------   -----------    
  2000                 菜系                   NULL  
  2001                 省                     NULL  
  2004                 市                     2001  
  2005                 县                     2004  
  2002                 车位                   NULL  
  2003                 人均消费               NULL  
  */  
   
   
  drop   function   f_str  
  drop   table   tTop

3 楼lsqkeke(可可)回复于 2006-03-03 17:00:29 得分 0

select    
  *  
  from   tb    
  order   by   case   when   name='菜系'   then   0    
  when   name='省'   then   1   else   2   end,  
  case   when   parentid   is   nul   then   99999  
  else   parentid   end  
  Top

4 楼mousefog(IT老鼠)回复于 2006-03-03 17:01:40 得分 0

结果不对啊Top

5 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2006-03-03 17:02:31 得分 0

create   table   t   (id   int,name   varchar(10),parentid   int)  
  insert   into   t   select   2000,'菜系         ',null  
  insert   into   t   select   2001,'省             ',null  
  insert   into   t   select   2002,'车位         ',null  
  insert   into   t   select   2003,'人均消费',null  
  insert   into   t   select   2004,'市             ',2001  
  insert   into   t   select   2005,'县             ',2004  
  go  
   
   
  create   function   f_str(@id   int)  
  returns   varchar(1000)  
  as  
  begin  
          declare   @ret   varchar(1000),@pid   int  
          select   @pid=parentid,@ret=right('0000'+rtrim(id),5)   from   t   where   id=@id  
          while   @pid   is   not   null  
          begin  
                  set   @id=@pid  
                  select   @pid=parentid,@ret=right('0000'+rtrim(id),5)+@ret   from   t   where   id=@id  
          end  
          return   @ret  
  end  
  go  
   
   
  select   *   from   t   order   by   dbo.f_str(id)  
  go  
   
  /*  
  id                     name               parentid          
  -----------   ----------   -----------    
  2000                 菜系                   NULL  
  2001                 省                     NULL  
  2004                 市                     2001  
  2005                 县                     2004  
  2002                 车位                   NULL  
  2003                 人均消费               NULL  
  */  
   
   
  drop   function   f_str  
  drop   table   tTop

6 楼mousefog(IT老鼠)回复于 2006-03-03 17:02:33 得分 0

不好意思,刚才看的时候下面的还没刷出来,先谢谢了Top

7 楼lsqkeke(可可)回复于 2006-03-03 17:02:54 得分 10

declare   @t   table(id   int,name   varchar(10),parentid   int)  
  insert   into   @t   select   2000,'菜系         ',null  
  insert   into   @t   select   2001,'省             ',null  
  insert   into   @t   select   2002,'车位         ',null  
  insert   into   @t   select   2003,'人均消费',null  
  insert   into   @t   select   2004,'市             ',2001  
  insert   into   @t   select   2005,'县             ',2004  
   
  select    
  *  
  from   @t  
  order   by   case   when   name='菜系'   then   0    
  when   name='省'   then   1   else   2   end,  
  case   when   parentid   is   null   then   99999  
  else   parentid   end  
   
  结果:  
    id       name           parentid  
  2000     菜系         NULL  
  2001     省             NULL  
  2004     市             2001  
  2005     县             2004  
  2002     车位         NULL  
  2003     人均消费 NULL Top

8 楼wgsasd311(自强不息)回复于 2006-03-03 17:05:22 得分 10

set   nocount   on  
  create   table   t   (id   int,name   varchar(10),parentid   int)  
  insert   into   t   select   2000,'菜系         ',null  
  insert   into   t   select   2001,'省             ',null  
  insert   into   t   select   2002,'车位         ',null  
  insert   into   t   select   2003,'人均消费',null  
  insert   into   t   select   2004,'市             ',2001  
  insert   into   t   select   2005,'县             ',2004  
  go  
  select    
  *  
  from   t   order   by   case   when   name='菜系'   then   0    
  when   name='省'   then   1   else   2   end,case   when   parentid   is   null   then   9999  
  else   parentid   end  
  /*  
  id                     name               parentid          
  -----------   ----------   -----------    
  2000                 菜系                   NULL  
  2001                 省                     NULL  
  2004                 市                     2001  
  2005                 县                     2004  
  2002                 车位                   NULL  
  2003                 人均消费               NULL  
  */  
  drop   table   t  
  Top

9 楼v888(aosgzi)回复于 2006-03-03 17:10:43 得分 0

1楼对地Top

相关问题

  • 求一SQL语句,急用,谢谢先~
  • 求一SQL语句 ,先谢了!
  • 求一个sql语句 先谢谢了
  • 一句sql
  • 一句sql
  • 一句sql
  • 求一sql
  • 一条sql
  • 一句sql
  • 一sql问题!

关键词

  • 消费
  • sql
  • null
  • nullinsert
  • 菜系
  • parentid
  • 车位
  • 省
  • 县
  • 人均消费

得分解答快速导航

  • 帖主:mousefog
  • libin_ftsafe
  • lsqkeke
  • wgsasd311

相关链接

  • SQL Server类图书

广告也精彩

反馈

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