id  name   sign
1   a       1
2   b       1
3   c       1
4   d       1
5   e       2
6   f       2select top 4* from test where sign in('1','2') 
我想得到sign等于1或2的前4条数据。现在因为sign=1的数据太多所以我取前4条导致没办法获得sign=2的数据了
 高手有什么办法没

解决方案 »

  1.   

    if object_id('tb')is not null drop table tb
    go
    create table tb(id int,  [name]  varchar(10), [sign] int) 
    insert tb select 1 , 'a' ,     1 
    insert tb select 2 , 'b' ,     1 
    insert tb select 3 , 'c'  ,    1 
    insert tb select 4 , 'd'  ,    1 
    insert tb select 5 , 'e'   ,   2 
    insert tb select 6,  'f'    ,  2
    select * from tb t where  id in (select top 2 id from tb where [sign]=t.[sign])
    /*id          name       sign        
    ----------- ---------- ----------- 
    1           a          1
    2           b          1
    5           e          2
    6           f          2*/
      

  2.   

    如果我要取200条,而sign在1到100氛围呢
     是不是也一样
      

  3.   


    create table test(id int,name varchar(10),[sign] int)
    insert test select 1,'a',1 union all
    select 2,'b',1 union all
    select 3,'c',1 union all
    select 4,'d',1 union all
    select 5,'e',2 union all
    select 6,'f',2 
    goselect * from test
    select * from test t where  id in (select top 2 id from test where [sign]=t.[sign])id          name       sign        
    ----------- ---------- ----------- 
    1           a          1
    2           b          1
    5           e          2
    6           f          2(所影响的行数为 4 行)
      

  4.   

    楼上几位 首先谢谢你id  name  sign 
    1  a      1 
    2  b      1 
    3  c      1 
    4  d      1 
    5  e      2 
    6  f      2 
    7  g      3
    8  h      3
    9  j      3我想获得 sign=1或 sign=2 或 sign=3的 前7条数据,该怎么写。
    要求这前7条数据中既有sign=1也有 sign=2 也有 sign=3的数据
      

  5.   

    if object_id('tb')is not null drop table tb
    go
    create table tb(id int,  [name]  varchar(10), [sign] int) 
    insert tb select 1 , 'a' ,     1 
    insert tb select 2 , 'b' ,     1 
    insert tb select 3 , 'c'  ,    1 
    insert tb select 4 , 'd'  ,    1 
    insert tb select 5 , 'e'   ,   2 
    insert tb select 6,  'f'    ,  2
    insert tb select 7,  'g' ,     3 
    insert tb select 8,  'h' ,     3 
    insert tb select 9,  'j' ,    3 
    select top 7 * from tb t where  id in (select top 3 id from tb where [sign]=t.[sign])
    /*id          name       sign        
    ----------- ---------- ----------- 
    1           a          1
    2           b          1
    3           c          1
    5           e          2
    6           f          2
    7           g          3
    8           h          3*/
      

  6.   

    楼上的大哥,
    你这样做好象保证不了 数据被截取吧。
    如果前7条数据都是 sign=1呢?而sign=2和sign=3的数据就没有了