表 tracing 有字段:ID,Type,nextUserID,Date
现要查出 nextUserID = 45,且以Type分类中 Date最大的ID Type NextUserID Date
1 1 45          2006-9-19
2 1 46          2006-9-18
3 2 45          2006-8-1
4 2 46          2006-8-2
5 3 45          2006-7-2
6 3 47          2006-7-1上面查出来的结果应该是
ID Type NextUserID Date
1 1 45          2006-9-19
5 3 45          2006-7-2

解决方案 »

  1.   

    select id,type,NextUserID,max(Date) from 表 whereNextUserID = 45 group by type,id,NextUserID
      

  2.   

    select t.id, t.type, t.nextUserID, max(Date) 
    from tracing t
    where t.nextUserID = 45 
    group by t.type
      

  3.   

    上面的大哥的不对
    按你的方法会查处下面结果
    但是 第二条是不对的
    ID Type NextUserID Date
    1 1 45          2006-9-19
    3 2 45          2006-8-1
    5 3 45          2006-7-2
      

  4.   

    select a.* 
    from tracing a,(
      select t.type,max(Date) maxDate
      from tracing t
      where t.nextUserID = 45 
      group by t.type
    ) tt
    where a..nextUserID = 45 and a.type=tt.type and a.Date=tt.maxDate
      

  5.   

    wiler(@_@) 
    做的也是出来跟上面同样的结果
    ID Type NextUserID Date
    1 1 45          2006-9-19
    3 2 45          2006-8-1
    5 3 45          2006-7-2
      

  6.   

    晕,你自己搞错了吧!3 2 45 2006-8-1这条记录也是符合你的条件的呢。
    nextUserID = 45,且以Type分类为2
    Date最大的
      

  7.   

    稍微改了一下 wiler(@_@) 的  出来了  
    select a.* 
    from tracing a,(
      select t.type,max(Date) maxDate
      from tracing t 
      group by t.type
    ) tt
    where a..nextUserID = 45 and a.type=tt.type and a.Date=tt.maxDate感谢wiler(@_@) !!  给分!!!!!!