CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  应用实例

此分类搜索的存储过程如何写?

楼主focusonline(Study C#)2005-01-24 12:37:41 在 MS-SQL Server / 应用实例 提问

数据库中有两张表,分别为NewsClass、News这两张表的结构如下:  
  NewsClass:  
  ClassID  
  Class  
   
  News:  
  NewsID  
  ClassID  
  Title  
   
  通过ClassID使两张表产生关系,现在想实现这样的功能:从News表中查找出每个类别的前十条记录,用存储过程输出给程序使用。 问题点数:50、回复次数:2Top

1 楼didoleo(冷月无声)回复于 2005-01-24 12:53:39 得分 5

--测试数据,假设newsid不重复  
  create   table   NewsClass  
  (ClassID   int,Class   varchar(100))  
   
  insert   into   NewsClass  
  select   1,'opq'   union   all  
  select   2,'rst'  
   
  create   table   News  
  (NewsID   int,ClassID   int,Title   varchar(100))  
   
  insert   into   News  
  select   1,1,'aaa'   union   all  
  select   2,1,'bbb'   union   all  
  select   3,1,'ccc'   union   all  
  select   4,1,'ddd'   union   all  
  select   5,1,'eee'   union   all  
  select   6,1,'fff'   union   all  
  select   7,1,'ggg'   union   all  
  select   8,1,'hhh'   union   all  
  select   9,1,'iii'   union   all  
  select   10,1,'jjj'   union   all  
  select   11,1,'kkk'   union   all  
  select   13,2,'aaaa'   union   all  
  select   14,2,'bbbb'   union   all  
  select   15,2,'cccc'   union   all  
  select   16,2,'dddd'   union   all  
  select   17,2,'eeee'   union   all  
  select   18,2,'ffff'   union   all  
  select   19,2,'gggg'   union   all  
  select   20,2,'hhhh'   union   all  
  select   21,2,'iiii'   union   all  
  select   22,2,'jjjj'   union   all  
  select   23,2,'kkkk'    
   
   
  select   a.*,b.class   from    
  (select   *   from   news   a   where    
        (select   count(1)   from   news   where   newsid<=a.newsid   and   classid=a.classid)   between   1   and   10)   a  
  left   join   newsclass   b   on   a.classid=b.classid  
  order   by   a.classid,a.newsid  
   
  ----------------------------------  
  1 1 aaa opq  
  2 1 bbb opq  
  3 1 ccc opq  
  4 1 ddd opq  
  5 1 eee opq  
  6 1 fff opq  
  7 1 ggg opq  
  8 1 hhh opq  
  9 1 iii opq  
  10 1 jjj opq  
  13 2 aaaa rst  
  14 2 bbbb rst  
  15 2 cccc rst  
  16 2 dddd rst  
  17 2 eeee rst  
  18 2 ffff rst  
  19 2 gggg rst  
  20 2 hhhh rst  
  21 2 iiii rst  
  22 2 jjjj rst  
   
   
  (所影响的行数为   20   行)Top

2 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2005-01-24 13:39:45 得分 45

select    
          a.*    
  from    
          News   a    
  where    
          a.NewsID   in(select    
                                            top   10   NewsID    
                                  from    
                                            News    
                                  where    
                                            ClassID=a.ClassID    
                                  order   by    
                                            NewsID   desc)Top

相关问题

  • 我的这个站内搜索引擎的存储过程怎么写?
  • 多关键字模糊搜索的存储过程应该怎样写?
  • 存储过程怎么写?
  • 存储过程怎么写??
  • 存储过程怎么写
  • 如何写存储过程
  • 存储过程怎么写?
  • 多级分类下的信息统计的存储过程如何写?
  • 存储过程再调存储过程!如何写啊?
  • 请教!通过textbox和dropdownlist进行分别搜索的存储过程怎么写呀?

关键词

  • union allselect
  • newsclass
  • newsid
  • 表
  • classid
  • select

得分解答快速导航

  • 帖主:focusonline
  • didoleo
  • libin_ftsafe

相关链接

  • SQL Server类图书

广告也精彩

反馈

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