有一个类别表,其中有3个类别记录,一张详情表取出每个类别下的五条,这样就会组成一个有15条的记录集
请问sql怎么写

解决方案 »

  1.   

    select top 5 * from b where type=1
    union
    select top 5 * from b where type=2
    union
    select top 5 * from b where type=3
      

  2.   

    select top 5 * from b where type=1
    union
    select top 5 * from b where type=2
    union
    select top 5 * from b where type=3
    =========================================
    这样是不行的,类别是个添加删除的!
    再求新法
      

  3.   

    select a.* from news a ,category c where id in(
    select  top 2 b.id from news b  where
    b.categoryid = c.categoryid 
    order by b.id desc
    )
      

  4.   

    你可以参考以下这个存储过程:CREATE proc up_GetIndexArticle@chvSpecialId_1 int=null,
    @chvSpecialId_2 int=null,
    @chvSpecialId_3 int=null
    as
    create  table #MyTableVar
    (ArticleId bigint,
     title nvarchar(500),
    channel char(100))
    if @chvSpecialId_1 is not null
    insert into #MyTableVar select top 5 *
    from V_ArticleSpecial where specialId=@chvSpecialId_1if @chvSpecialId_2 is not null
    insert into #MyTableVar select top 5 *
    from V_ArticleSpecial where specialId=@chvSpecialId_2if @chvSpecialId_3 is not null
    insert into #MyTableVar *
    from V_ArticleSpecial where specialId=@chvSpecialId_3select * from #MyTableVar order by ArticleId desc
    drop table #MyTableVarGO