CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

怎么查找无级分类中一基础大类下子类的最新信息

楼主hdyong(无痕)2005-06-03 09:40:25 在 MS-SQL Server / 基础类 提问

数据库有两个表一个是class(无级分类的),一个是info表。  
   
  class表  
  ------------------  
  id   parent_id   classname   islast  
  1         0                 类一             1  
  2         1                 类二             0  
  3         1                 类三             1  
  4         2                 类四             0  
  5         4                 类五             1  
   
  info表  
  ------------------  
  id   classid   name  
  1         1             X  
  2         2             T  
  3         1             Y  
  4         2             Z  
  5         3             C  
  6         1             T  
   
  我想生成这样一页,查找基础类1下面的所有子类,如果该子类是最终类就查找最新的5条,如果该子类不是最终类,那就从该子类的子类中查找最新的5条记录  
  基础类1  
  __________________________________  
   
  类二                     类三        
  _________________________________  
  问题点数:50、回复次数:10Top

1 楼zjcxc(邹建)回复于 2005-06-03 10:00:30 得分 50

--查询处理的函数  
  create   function   f_cid(@id   int)  
  returns   @re   table(id   int,level   int,islast   bit)  
  as  
  begin  
  declare   @l   int  
  set   @l=0  
  insert   @re   select   id,@l,islast   from   class   where   id=@id  
  while   @@rowcount>0  
  begin  
  set   @l=@l+1  
  insert   @re   select   a.id,@l,a.islast  
  from   class   a,@re   b  
  where   a.parent_id=b.id   and   b.level=@l-1  
  end  
  delete   from   @re   where   islast=1  
  end  
  go  
   
  --调用函数实现查询(查询基础1)  
  select   top   5   a.*   from   info   i,f_cid(1)   b   where   a.classid=b.classid  
   
  Top

2 楼hdyong(无痕)回复于 2005-06-03 11:01:35 得分 0

显示有错误,说是最后一条语句必须是返回语句。Top

3 楼zjcxc(邹建)回复于 2005-06-03 11:14:01 得分 0

--查询处理的函数  
  create   function   f_cid(@id   int)  
  returns   @re   table(id   int,level   int,islast   bit)  
  as  
  begin  
  declare   @l   int  
  set   @l=0  
  insert   @re   select   id,@l,islast   from   class   where   id=@id  
  while   @@rowcount>0  
  begin  
  set   @l=@l+1  
  insert   @re   select   a.id,@l,a.islast  
  from   class   a,@re   b  
  where   a.parent_id=b.id   and   b.level=@l-1  
  end  
  delete   from   @re   where   islast=1  
  return     --少了  
  end  
  go  
   
  --调用函数实现查询(查询基础1)  
  select   top   5   a.*   from   info   i,f_cid(1)   b   where   a.classid=b.classid  
   
   
  Top

4 楼hdyong(无痕)回复于 2005-06-03 11:20:26 得分 0

还是出不来。  
  我把select   top   5   a.*   from   info   i,f_cid(1)   b   where   a.classid=b.classid  
  改成  
  select   top   5   a.*   from   info   a,f_cid(1)   b   where   a.classid=b.id  
  是通过了,但是什么也查不出来。Top

5 楼zjcxc(邹建)回复于 2005-06-03 11:24:16 得分 0

--1.  
  select   f_cid(1)   --看看是否查出了1的所有子类  
   
  --2.你列的示例数据有些不对吧?   id=1的,它应该不是最末级,islast怎么会是1呢?Top

6 楼zjcxc(邹建)回复于 2005-06-03 11:26:52 得分 0

create   table   class(id   int,parent_id   int,classname   varchar(10),islast   bit)  
  insert   class   select   1,0,'类一',1  
  union   all   select   2,1,'类二',0  
  union   all   select   3,1,'类三',1  
  union   all   select   4,2,'类四',0  
  union   all   select   5,4,'类五',1  
   
  create   table   info(id   int,classid   int,name   varchar(10))  
  insert   info   select   1,1,'X'  
  union   all   select   2,2,'T'  
  union   all   select   3,1,'Y'  
  union   all   select   4,2,'Z'  
  union   all   select   5,3,'C'  
  union   all   select   6,1,'T'  
  go  
   
  --查询处理的函数  
  create   function   f_cid(@id   int)  
  returns   @re   table(id   int,level   int,islast   bit)  
  as  
  begin  
  declare   @l   int  
  set   @l=0  
  insert   @re   select   id,@l,islast   from   class   where   id=@id  
  while   @@rowcount>0  
  begin  
  set   @l=@l+1  
  insert   @re   select   a.id,@l,a.islast  
  from   class   a,@re   b  
  where   a.parent_id=b.id   and   b.level=@l-1  
  end  
  delete   from   @re   where   islast=0  
  return  
  end  
  go  
   
  --调用函数实现查询(查询基础1)  
  select   top   5   a.*   from   info   a,f_cid(1)   b   where   a.classid=b.id  
  go  
   
  --删除测试  
  drop   table   class,info  
  drop   function   f_cid  
   
  /*--结果  
   
  id                     classid           name                
  -----------   -----------   ----------    
  1                       1                       X  
  3                       1                       Y  
  5                       3                       C  
  6                       1                       T  
   
  (所影响的行数为   4   行)  
   
  --*/Top

7 楼zjcxc(邹建)回复于 2005-06-03 11:27:21 得分 0

数据删除的写反了,正确的如上Top

8 楼hdyong(无痕)回复于 2005-06-03 12:02:14 得分 0

不好意思,前面的数据是有错误。  
  Top

9 楼hdyong(无痕)回复于 2005-06-03 13:06:14 得分 0

class表  
  ------------------  
  id   parent_id   classname   islast  
  1         0                 类一             0  
  2         1                 类二             0  
  3         1                 类三             1  
  4         2                 类四             0  
  5         4                 类五             1  
  5         1                 类六             1  
   
  info表  
  ------------------  
  id   classid   name  
  1         1             X  
  2         2             T  
  3         1             Y  
  4         2             Z  
  5         3             C  
  6         1             T  
   
  我想查寻得到的结果在显示的时候多一个数据项classid2,来显示他们是属于类一下的哪个字类的的classid,  
  也就是这样的,那应该怎么修改?  
  id                     classid           name                
  -----------   -----------   ----------   ----------    
  1                       1                       X                            
  3                       1                       Y  
  5                       3                       C  
  6                       1                       TTop

10 楼hdyong(无痕)回复于 2005-06-03 13:10:44 得分 0

修改了一下,  
   
  class表  
  ------------------  
  id   parent_id   classname   islast  
  1         0                 类一             0  
  2         1                 类二             0  
  3         1                 类三             1  
  4         2                 类四             0  
  5         4                 类五             1  
  6         1                 类六             1  
   
  info表  
  ------------------  
  id   classid   name  
  1         3             X  
  2         5             T  
  3         4             Y  
  4         6             Z  
  5         3             C  
  6         5             T  
   
  我想查寻得到的结果在显示的时候多一个数据项classid2,来显示他们是属于"类一"下的哪个子类的的classid,  
  也就是这样的  
  id                     classid           name                   classid2        
  -----------   -----------   ----------   ----------    
  1                       3                       X                                 3                                              
  3                       4                       Y                                 2  
  5                       3                       C                                 3  
  6                       5                       T                                 2Top

相关问题

  • 子类、父类
  • 关于子类化
  • 子类化==超类化?
  • 父类和子类调用
  • 子类化的问题(vc++)
  • 派生子类的问题:
  • 关于Listbox的子类化
  • 关于子类问题
  • 子类如何使用和子类同名的基类方法?
  • 请问如何从classWizard中派生子类的子类

关键词

  • 函数
  • 查询
  • 子类
  • islast
  • 类
  • classid
  • 查找
  • cid
  • beginset
  • asbegindeclare

得分解答快速导航

  • 帖主:hdyong
  • zjcxc

相关链接

  • SQL Server类图书

广告也精彩

反馈

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