select top 7 B.catname,A.* from category A left join article B
on A.catid=B.catid
请试一试!

解决方案 »

  1.   

    上条写错了:
    select top 7 B.catname,A.* from article A left join category B
    on A.catid=B.catid
      

  2.   

    --try
    select top 7 * from [article]
    where catid=(select catid from [category] where catname like '%诗歌%')
      

  3.   

    select
        a.*
    from
        article  a,
        category c
    where
        a.catid = c.catid 
        and
        c.catname in('诗歌','小说','散文')
        and
        a.id in(select top 7 id from article where catid=a.catid)
    order by
        a.catid,a.id
      

  4.   

    --如果是同时选择两个类别以上,就这样一来
    --创建测试环境
    create table [article] (id int,catid int,author varchar(5),title varchar(10),body varchar(200))
    insert into [article] select 1,2,'鲁迅','狂人日记','今天我吃人了'
    union all select 2,1,'冯至','十四行集','给我狭窄的心,一个大的宇宙'
    union all select 3,1,'于坚','避雨之树','大雨……'create table [category] (catid int,catname varchar(10))
    insert into [category] select 1,'诗歌'
    union all select 2,'小说'
    union all select 3,'散文'
    union all select 4,'戏剧'--查询语句
    select top 7 * from [article]
    where catid in (select catid from [category] where charindex(','+catname+',',',诗歌,小说,')>0)--结果id          catid       author title      body   
    ----------- ----------- ------ --------
    1           2           鲁迅     狂人日记       今天我吃人了
    2           1           冯至     十四行集       给我狭窄的心,一个大的宇宙
    3           1           于坚     避雨之树       大雨……(所影响的行数为 3 行)--删除测试环境
    drop table [article]
    drop table [category]
      

  5.   

    select * from [article] t
    where (select count(1) from [article] where catid=t.catid and id<=t.id)<=7
    order by catid,id
      

  6.   


    select top 7 B.catname,A.* from article A left join category B
    on A.catid=B.catid where catname like'%诗歌%'
    union all 
    select top 7 B.catname,A.* from article A left join category B
    on A.catid=B.catid where catname like'%散文%'
    union all 
    select top 7 B.catname,A.* from article A left join category B
    on A.catid=B.catid where catname like'%小说%'
    union all 
    select top 7 B.catname,A.* from article A left join category B
    on A.catid=B.catid where catname like'%戏剧%'
    ……
      

  7.   

    如果只选择诗歌小说散文:
    select * from [article] t
    where (select count(1) from [article] where catid=t.catid and id<=t.id)<=7 and
          catid in (select catid from [category] where catname in ('诗歌','小说','散文'))
    order by catid,id
      

  8.   

    select top 0 * into #article from articledeclare curA cursor for 
    select catid from category where catname in ('诗歌','小说')
    declare @catid varchar(20)
    open curAfetch next from curA into @catid
    while @@fetch_status = 0
    begin
    insert into #article select top 7 * from article where catid=@catid
    fetch next from curA into @catid
    end---楼主要的结果
    select * from #article