--如果ID唯一,结果正确,效率不敢保证
select * from news表 where 
id in(select top 10 id from news表 order by id) or
id in(select top 10 id from news表 order by newstype) or
id in(select top 10 id from news表 order by title) or
id in(select top 10 id from news表 order by content) or

解决方案 »

  1.   

    select * from news表 where 
    id in(select top 10 id from news表 order by id) or
    id in(select top 10 id from news表 order by newstype) or
    id in(select top 10 id from news表 order by title) or
    id in(select top 10 id from news表 order by content) or
    //in的效率。select * from (select top 10 * from news表 order by id) a
    union all
    select * from (select top 10 * from news表 order by newstype)b
    union all
    select * from (select top 10 * from news表 order by title) c
    union all
    select * from (select top 10 * from news表 order by content) d
      

  2.   

    njz168(飞龙在天) 
    能说一下原理吗,谢谢。
      

  3.   

    如果id是递增列的话,可以这样:
    ----创建测试数据
    declare @news table(id int,newstype varchar(10),title varchar(100),content varchar(2000))
    insert @news
    select 1,'体育新闻','标题1','内容1' union all
    select 2,'体育新闻','标题2','内容2' union all
    select 3,'体育新闻','标题3','内容3' union all
    select 4,'体育新闻','标题4','内容4' union all
    select 5,'体育新闻','标题5','内容5' union all
    select 6,'体育新闻','标题6','内容6' union all
    select 7,'体育新闻','标题7','内容7' union all
    select 8,'体育新闻','标题8','内容8' union all
    select 9,'体育新闻','标题9','内容9' union all
    select 10,'体育新闻','标题10','内容10' union all
    select 11,'体育新闻','标题11','内容11' union all
    select 12,'体育新闻','标题12','内容12' union all
    select 13,'财经新闻','标题1','内容1' union all
    select 14,'财经新闻','标题2','内容2' union all
    select 15,'科技新闻','标题1','内容1'
    ----方法一
    select * from @news a 
    where (select count(*) from @news where newstype = a.newstype and id < a.id) < 10
    ----方法二
    select * from @news a
    where not exists(select 1 from @news where newstype = a.newstype and id < a.id group by newstype having count(*) > 9)
    ----方法三
    select * from @news a
    where exists(select 1 from @news where newstype = a.newstype and id <= a.id group by newstype having count(*) <= 10)
    ----方法四
    select * from @news a
    where id in (select top 10 id from @news where newstype = a.newstype order by id)
      

  4.   

    hellowork(一两清风) 可能我上面的问题描述有问题,导致你理解错了。
      

  5.   

    newstype就应该是新闻类别,因此以为是求每类新闻的前十条...
      

  6.   


    --这样效率会高一些,而且重复的数据可以过滤掉
    select id,newstype,title,content from (select top 10 * from news表 order by id) a
    union
    select id,newstype,title,content from (select top 10 * from news表 order by newstype)b
    union
    select id,newstype,title,content from (select top 10 * from news表 order by title) c
    union
    select id,newstype,title,content from (select top 10 * from news表 order by content) d
      

  7.   

    这样对吗。
    select id,newstype,title,content from (select top 10 * from news表 where newstype=1 order by id desc) a
    union
    select id,newstype,title,content from (select top 10 * from news表 where newstype=2  order by id desc)b
    union
    select id,newstype,title,content from (select top 10 * from news表 where newstype=4 order by id desc) c
    union
    select id,newstype,title,content from (select top 10 * from news表 where newstype=8 order by id desc) d但愿有其他更有效率的方法。
    谢谢。
      

  8.   

    ----方法一
    select * from @news a 
    where (select count(*) from @news where newstype = a.newstype and id > a.id) < 10
    and newstype in(1,2,4,8)
    ----方法二
    select * from @news a
    where not exists(select 1 from @news where newstype = a.newstype and id > a.id group by newstype having count(*) > 9)
    and newstype in(1,2,4,8)
    ----方法三
    select * from @news a
    where exists(select 1 from @news where newstype = a.newstype and id >= a.id group by newstype having count(*) <= 10)
    and newstype in(1,2,4,8)
    ----方法四
    select * from @news a
    where id in (select top 10 id from @news where newstype = a.newstype order by id desc)
    and newstype in(1,2,4,8)
      

  9.   

    hellowork(一两清风) 
    好你的方法,我明天试试。
    谢谢。
      

  10.   

    hellowork(一两清风) 
    ----方法一
    select * from @news a 
    where (select count(*) from @news where newstype = a.newstype and id > a.id) < 10
    and newstype in(1,2,4,8)这一种方法效率最高。谢谢。