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

查询限制问题?没办法了!

楼主moon15()2003-12-01 22:46:22 在 MS-SQL Server / 基础类 提问

有a表和b表  
        id|     type                           id       a_id     name    
  -----|----------                            
      1     |       水果                         1         1             香蕉  
      2     |       蔬菜                         2         1             苹果  
      3     |       水产类                     3         2             芹菜  
      .                 .                           .         .                 .  
      .                 .                           .         .                 .  
  我只想查寻出a表每个类别对应b表的9条纪录,也就是我只想显示出,水果类的9条纪录,蔬菜类的9条纪录.... 问题点数:40、回复次数:20Top

1 楼youngby(C-love)回复于 2003-12-01 23:14:57 得分 0

应该是吧:  
    id|     type                           a_id     id     name    
  -----|----------                            
      1     |       水果                         1         1             香蕉  
      2     |       蔬菜                         2         1             苹果  
      3     |       水产类                     3         2             芹菜  
      .                 .                           .         .                 .  
  Top

2 楼youngby(C-love)回复于 2003-12-02 00:23:41 得分 0

偶很菜,  
  select   top   9   name   from   b   where   id=2   UNION     select   top   9   name   from   b   where   id=1Top

3 楼teaism()回复于 2003-12-02 08:39:08 得分 0

select   A.id,A.type,B.id.B.name   from   tableA   A,tableB   B    
  where   A.id=B.aid    
  and   B.id   in   (select   top   9   id   from   tableB   where   aid=A.id)Top

4 楼zjcxc(邹建)回复于 2003-12-02 08:44:40 得分 0

select   b.*    
  from   a表   a   join   b表   b   on   a.id=b.a_id  
  where   a.type   in('水果','蔬菜')  
  order   by   a.idTop

5 楼victorycyz(--)回复于 2003-12-02 09:01:59 得分 0

先markTop

6 楼pengdali()回复于 2003-12-02 09:05:02 得分 10

select   *   from   b   tem   where   id   in   (select   top   9   id   from   b   where   a_id=tem.a_id)Top

7 楼pengdali()回复于 2003-12-02 09:05:43 得分 0

或:  
   
  select   *   from   b   tem   where   a_id   in   (select   id   from   a)   and   id   in   (select   top   9   id   from   b   where   a_id=tem.a_id)Top

8 楼wzh1215(懒猫)回复于 2003-12-02 09:17:37 得分 0

select   b.*   from   a,b   where   a.id=b.id   and   id   in   (select   top   9   id   from   b,a   where   a.id=b.id)Top

9 楼zjcxc(邹建)回复于 2003-12-02 09:20:43 得分 0

--搞错了,是每类前9条记录  
   
  楼上几位的才是正确的.Top

10 楼victorycyz(--)回复于 2003-12-02 09:44:24 得分 0

现在我知道了,按大力的方法测试通过。  
   
  楼主请暂不要结贴。我问一个问题。  
  Top

11 楼victorycyz(--)回复于 2003-12-02 09:52:32 得分 0

就此问题,我建了一个表T1,字段:id,qty,ftype。我想根据ftype的值,每个值取前9行,为此创建一个函数:  
   
  create   function   GetFixRowID   (@intNum   int,@strType   varchar(2))  
  returns   int     --这是第2行  
  as  
  begin  
      declare   @intOutID     int  
      set   @intOutID=(select   top   1   T.id   from   (select   top   @intNum   a.id   from   t1   a   where      
                                    a.ftype=@strtype)   T   order   by   T.id   desc   )       --这是第6行。  
      return   (@intoutid)  
  end  
  go  
   
  select   *   from   t1   where   id<=GetFixRowID(9,ftype)   order   by   ftype,id  
  go  
   
  报告错误:  
  服务器:   消息   170,级别   15,状态   1,过程   GetFixRowID,行   6  
  第   6   行:   '@intNum'   附近有语法错误。  
  服务器:   消息   195,级别   15,状态   10,行   2  
  'GetFixRowID'   不是可以识别的   函数名。  
   
   
  我不知道为什么错了,高手请指教。谢谢。  
   
  (很笨拙的函数,让大家见笑了)Top

12 楼shuiniu(飞扬的梦)(我是一头只吃西红柿的水牛)回复于 2003-12-02 10:29:22 得分 0

ORDER   BY   子句  
  指定结果集的排序。除非同时指定了   TOP,否则   ORDER   BY   子句在视图、内嵌函数、派生表和子查询中无效。  
  --------  
  create   function   GetFixRowID   (@intNum   int,@strType   varchar(2))  
  returns   int     --这是第2行  
  as  
  begin  
      declare   @intOutID     int  
      set   @intOutID=(select   top   1   T.id   from   (select   top   @intNum   a.id   from   t1   a   where      
                                    a.ftype=@strtype   order   by   a.id   --这儿)   T   order   by   T.id   desc   )       --这是第6行。                                                                
      return   (@intoutid)  
  end  
  go  
   
  select   *   from   t1   where   id<=GetFixRowID(9,ftype)   order   by   ftype,id  
  go  
  Top

13 楼moon15()回复于 2003-12-02 10:32:08 得分 0

to:youngby(诗人:$(!@杰拉*…^%正版男生#@)   你把我的表结构给弄错了,  
  有a表和b表  
        id|     type                           id       a_id     name    
  -----|----------                            
      1     |       水果                         1         1             香蕉  
      2     |       蔬菜                         2         1             苹果  
      3     |       水产类                     3         2             芹菜  
      .                 .                           .         .                 .  
      .                 .                           .         .                 .  
  这样的才是对的,你再想想。  
  我的查询开始是这样的  
  slelect   *   from   b   inner   join   a   on   a.id=b.a_id   order   by   a.id  
  这样查询出的结果只是列出每个类别的所有纪录。并没有限制列出每个类只要9条纪录。  
  比如说象上面的表就是列出了水果类的   所有纪录,没有限制只列出9个水果名。  
  -------------------------------------------------------------------------  
  试了下大家给我的sql语句好像都有问题,   pengdali(大力   V3.0)   的好像只列出a表的类别纪录,就是只列出   水果、蔬菜等类别。并没有显示出   每个类的   品名。其它的也基本如此。不知道是不是我陈述问题的意思不清楚,还是大家理解错了。  
  谢谢大家。Top

14 楼zjcxc(邹建)回复于 2003-12-02 10:36:37 得分 0

 
  select   a.type,b.name  
  from   a   join   b   on   a.id=b.a_id  
  where   b.id   in(select   top   9   id   from   b   where   a_id=a.id)Top

15 楼zjcxc(邹建)回复于 2003-12-02 10:42:44 得分 0

--下面是数据测试  
   
  --测试数据  
  declare   @a   table(id   int,type   varchar(10))  
  declare   @b   table(id   int   identity(1,1),a_id   int,name   varchar(10))  
   
  insert   into   @a  
  select   1,'水果'  
  union   all   select   2,'蔬菜'  
  union   all   select   3,'水产类'  
   
  insert   into   @b  
  select   1,'香蕉'  
  union   all   select   1,'苹果'  
  union   all   select   2,'芹菜'  
  union   all   select   1,'苹果a'  
  union   all   select   2,'芹菜b'  
  union   all   select   1,'苹果c'  
  union   all   select   2,'芹菜d'  
  union   all   select   1,'苹果e'  
  union   all   select   2,'芹菜f'  
  union   all   select   1,'苹果g'  
  union   all   select   2,'芹菜h'  
  union   all   select   1,'苹果1'  
  union   all   select   2,'芹菜2'  
  union   all   select   1,'苹果3a'  
  union   all   select   2,'芹菜4b'  
  union   all   select   1,'苹果5c'  
  union   all   select   2,'芹菜6d'  
  union   all   select   1,'苹果7e'  
  union   all   select   2,'芹菜8f'  
  union   all   select   1,'苹果9g'  
  union   all   select   2,'芹菜0h'  
  union   all   select   1,'苹果aa'  
  union   all   select   2,'芹菜bb'  
  union   all   select   1,'苹果acc'  
  union   all   select   2,'芹菜bdd'  
  union   all   select   1,'苹果cee'  
  union   all   select   2,'芹菜dff'  
  union   all   select   1,'苹果egg'  
  union   all   select   2,'芹菜fhh'  
  union   all   select   1,'苹果gkk'  
  union   all   select   2,'芹菜hll'  
   
  --查询处理  
  select   *  
  from   @a   a   join   @b   b   on   a.id=b.a_id  
  where   b.id   in(select   top   9   id   from   @b   where   a_id=a.id)  
   
  /*--测试结果  
   
  id                     type               id                     a_id                 name                
  -----------   ----------   -----------   -----------   ----------    
  1                       水果                   1                       1                       香蕉  
  1                       水果                   2                       1                       苹果  
  1                       水果                   4                       1                       苹果a  
  1                       水果                   6                       1                       苹果c  
  1                       水果                   8                       1                       苹果e  
  1                       水果                   10                     1                       苹果g  
  1                       水果                   12                     1                       苹果1  
  1                       水果                   14                     1                       苹果3a  
  1                       水果                   16                     1                       苹果5c  
  2                       蔬菜                   3                       2                       芹菜  
  2                       蔬菜                   5                       2                       芹菜b  
  2                       蔬菜                   7                       2                       芹菜d  
  2                       蔬菜                   9                       2                       芹菜f  
  2                       蔬菜                   11                     2                       芹菜h  
  2                       蔬菜                   13                     2                       芹菜2  
  2                       蔬菜                   15                     2                       芹菜4b  
  2                       蔬菜                   17                     2                       芹菜6d  
  2                       蔬菜                   19                     2                       芹菜8f  
   
  (所影响的行数为   18   行)  
  --*/  
  Top

16 楼zjcxc(邹建)回复于 2003-12-02 10:43:38 得分 0

--如果要排序:  
  select   a.type,b.name   --如果显示所有字段,将a.type,b.name改为*  
  from   a   join   b   on   a.id=b.a_id  
  where   b.id   in(select   top   9   id   from   b   where   a_id=a.id)  
  order   by   a.id,b.idTop

17 楼zjcxc(邹建)回复于 2003-12-02 11:05:05 得分 0

函数中不能完成你的工作.  
   
  top   后面不能跟变量,必须是指定的数字.  
   
  而函数又不支持exec  
  Top

18 楼zjcxc(邹建)回复于 2003-12-02 11:05:14 得分 30

未在下面的列表中列出的语句不能用在函数主体中。    
   
  赋值语句。  
   
   
  控制流语句。  
   
   
  DECLARE   语句,该语句定义函数局部的数据变量和游标。  
   
   
  SELECT   语句,该语句包含带有表达式的选择列表,其中的表达式将值赋予函数的局部变量。  
   
   
  游标操作,该操作引用在函数中声明、打开、关闭和释放的局部游标。只允许使用以   INTO   子句向局部变量赋值的   FETCH   语句;不允许使用将数据返回到客户端的   FETCH   语句。  
   
   
  INSERT、UPDATE   和   DELETE   语句,这些语句修改函数的局部   table   变量。  
   
   
  EXECUTE   语句调用扩展存储过程。    
   
  Top

19 楼zjcxc(邹建)回复于 2003-12-02 11:16:10 得分 0

--想出了另一个办法,将函数改成这样就可以了.  
  create   function   GetFixRowID   (  
  @Num   int,  
  @Type   varchar(2))  
  returns   int  
  as  
  begin  
  declare   @ID     int  
  select   @id=id   from   t1   a  
  where     ftype=@type    
  and(select   sum(1)   from   t1   where   ftype=@type   and   id<=a.id)=@num  
  return   (@id)  
  end  
  go  
   
  select   *   from   t1   where   id<=dbo.GetFixRowID(9,ftype)   order   by   ftype,id  
  goTop

20 楼victorycyz(--)回复于 2003-12-02 13:49:41 得分 0

我的问题解决了。谢谢。Top

相关问题

  • 晕倒,两个库有没有办法做交叉查询的?
  • 请问嵌套查询有没有更简单的办法?
  • count(id) 查询没有办法用inner join吗?
  • 有没有办法限制虚函数可重载的层数?
  • 有没有办法限制SQL客户端的使用?
  • 有没有办法限制下拉框一次显示的项?
  • 有没有办法限制进程的CPU使用率
  • 查询分析器的字符限制?
  • SQL 查询的长度限制
  • 请问有没有办法只查询一个结果集中的前十行?

关键词

  • top
  • intoutid
  • ftype
  • tem
  • 蔬菜
  • 水果
  • 纪录
  • where
  • 问题
  • select top

得分解答快速导航

  • 帖主:moon15
  • pengdali
  • zjcxc

相关链接

  • SQL Server类图书

广告也精彩

反馈

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