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

解决方案 »

  1.   

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

  2.   

    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)
      

  3.   

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

  4.   

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

  5.   

    或: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)
      

  6.   

    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)
      

  7.   

    就此问题,我建了一个表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
    goselect * from t1 where id<=GetFixRowID(9,ftype) order by ftype,id
    go报告错误:
    服务器: 消息 170,级别 15,状态 1,过程 GetFixRowID,行 6
    第 6 行: '@intNum' 附近有语法错误。
    服务器: 消息 195,级别 15,状态 10,行 2
    'GetFixRowID' 不是可以识别的 函数名。
    我不知道为什么错了,高手请指教。谢谢。(很笨拙的函数,让大家见笑了)
      

  8.   

    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
    goselect * from t1 where id<=GetFixRowID(9,ftype) order by ftype,id
    go
      

  9.   

    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表的类别纪录,就是只列出 水果、蔬菜等类别。并没有显示出 每个类的 品名。其它的也基本如此。不知道是不是我陈述问题的意思不清楚,还是大家理解错了。
    谢谢大家。
      

  10.   


    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)
      

  11.   

    --下面是数据测试--测试数据
    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 行)
    --*/
      

  12.   

    --如果要排序:
    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.id
      

  13.   

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

  14.   

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

  15.   

    --想出了另一个办法,将函数改成这样就可以了.
    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
    goselect * from t1 where id<=dbo.GetFixRowID(9,ftype) order by ftype,id
    go