表tab
 col1   col1  col2 
  1      1     ab
  2      1     bc
  3      1     bd
  4      2     hu
  5      2     nj
  6      2     ih
  7      3     ha
  8      3     xj
  9      3     ih
 10      3     hc可以用group by得到以下结果 或者其它方法
col1   col1  col2 
  1      1     ab
  2      1     bc
  4      2     hu
  5      2     nj
  7      3     ha
  8      3     xj

解决方案 »

  1.   

    你的前两列名字一样,改第一个为col0 了
    select *
    from tab a
    where 2>(select count(*) from tab where col1=a.col1 and col0<t.col0)
      

  2.   

    col3  col1  col2 
      1      1    ab 
      2      1    bc 
      3      1    bd 
      4      2    hu 
      5      2    nj 
      6      2    ih 
      7      3    ha 
      8      3    xj 
      9      3    ih 
    10      3    hc
    select a.col1,a.col2,a.col3 from tt a left join tt b
    on a.col1=b.col1 and a.col3>=b.col3 a.col1,a.col2,a.col3 group by having count(b.col3)<=2
      

  3.   

    select a.col1,a.col2,a.col3 from tt a left join tt b 
    on a.col1=b.col1 and a.col3>=b.col3 group by a.col1,a.col2,a.col3
    having count(b.col3) <=2
      

  4.   


    SELECT * FROM tab t1 
    WHERE 3>(SELECT COUNT(*) FROM tab WHERE col1=t1.col1 AND col0<=t1.col0)
    ORDER BY t1.col1,t1.col0;
      

  5.   


    N>(select count(*) .会快一些。