现在有1个集合,比如 id(1,2,3,4,5,6)现在要从这些集合里 从数据库取 没个对应值的3个记录。我现在是这样做的
select top 3 * from 表 where id = 1
select top 3 * from 表 where id = 2
...
有没有一句话搞定的?
select top 18 * from 表 where id in (1,2,3,4,5,6)
比行哦谢谢~

解决方案 »

  1.   

    select top 3 * from 表 where id = 2
    要有个规则吧
    比如什么排序的?
      

  2.   

    select *
    from 表 t
    where [time] in(select top 3 [time] from 表 where id=t.id order by [time] desc)
    and id (1,2,3,4,5,6)
    --PS假设以时间排序
      

  3.   

    --or
    select *
    from 表 t
    where (select count(*) from 表 where id=t.id [time]>t.[time])<2
    and id (1,2,3,4,5,6)
    --PS假设以时间排序
      

  4.   


    --可以这么写:
    select b.* 
    from (
    select distinct id from 表 where id in (1,2,3,4,5,6)
    ) a 
    cross apply (select top (3) * from 表 where id = a.id) b
    记得结贴哦