有什么规则?自己加条件就行了。SELECT * FROM TableName WHERE ...^_^

解决方案 »

  1.   


    select a.*
    from T1 a, (select mainid, max(date) date from T1 group by mainid)b
    where a.mainid=b.mainid and a.date=b.date
      

  2.   

    select max(id),mainid,date,bz from T1 group by mainid HAVING count(mainid) = 2试一下
      

  3.   


    select * from table1 where date in (select max(date) date from table1 
    group by mainid ) order by id
      

  4.   

    select * from t1 where id = mainid*2呵呵,不知楼主有什么要求;这只是随便写写~~*^O^*
      

  5.   

    select * from T1 t
    where not exists(select 1 from T1 where mainid=t.mainid and datediff(day,t.[date],[date])>0)
      

  6.   

    --创建测试环境
    create table T1
    (
      ID int,
      mainid int,
      [date] varchar(10),
      bz     varchar(10)
    )
    insert T1
    select 1,1,'2005-1-1','bz1' union
    select 2,1,'2005-2-1','bz2' union
    select 3,2,'2005-1-1','bz3' union
    select 4,2,'2005-7-1','bz4' union
    select 5,2,'2005-6-1','bz5'--测试
    select * from T1 t
    where not exists(select 1 from T1 where mainid=t.mainid and datediff(day,t.[date],[date])>0)--删除测试环境
    drop table T1--结果
    /*
    ID          mainid      date       bz         
    ----------- ----------- ---------- ---------- 
    2           1           2005-2-1   bz2
    4           2           2005-7-1   bz4(所影响的行数为 2 行)
    */