我想选择一个表中,同一类中时间最大的一批记录。
例如:选择 时间字段(date) 最接近现在时间的而 A字段要唯一 的那些记录

解决方案 »

  1.   

    好像很简单,不是我理解错了吧
    select A字段, max(时间字段) from your_table group by A字段
    如果有条件,自己再加上where就可以了
      

  2.   

    时间字段(date) 最接近现在时间一批记录??,接近到什么程度撒?
      

  3.   

    TO 阿宝:我是想把除了A字段外还有几个字段都选出来,如B,C,D,现在只可以选择A字段和最大的时间,请问可以选择出除了A字段以外的B,C,D,字段吗?
      

  4.   

    可以,不过我想了解你的表结构是什么样子的,主键是什么,又没有可能出现以下情况:
    A字段, 时间字段,   B字段, C字段, D字段
    A      2004-06-23  B      C      D
    A      2004-06-23  BB     CC     DD
    如果出现这种情况,是2条都选还是只选1条,选那一条?
      

  5.   

    TO 阿宝:先谢谢您
    我的表结构是:
    A字段,日期字段,   时间字段,B字段,C字段,D字段   编号
    A     2004-06-23   10:50    B      C     D        1   
    A     2004-06-23   10:51    B1     C1    D1       2
    A1    2004-06-23   10:50    B2     C2    D2       3
    A1    2004-06-23   10:52    B3     C3    D3       4
    我要选的记录是2,4,就是选所有字段
      

  6.   

    select * from your_table  where 时间字段 in
    select max(时间字段) from your_table group by A字段
      

  7.   

    select * from your_table  where 
    (时间字段 in select max(时间字段) from your_table group by A字段 )
    and 
    (A字段 in select A字段 from your_table group by A字段 )
      

  8.   

    select A字段, 時間字段
    from your_table t
    where 時間字段 = (select max(時間字段) 
                       from your_table
                      where t.A字段=A字段)
    order by A字段
      

  9.   

    试试
    select a.A字段,a.日期字段, a.时间字段,a.B字段,a.C字段,a.D字段, a.编号
      from your_table a
     where a.日期字段 = (select max(日期字段) from your_table where A字段 = a.A字段)
       and b.时间字段 = (select max(时间字段) from your_table where 日期字段 = a.日期字段)
    我的还是觉得把(日期字段+时间字段)进行判断是最快的,都转成字符串再加,再MAX
      

  10.   

    好的 谢谢我测试出来了,
    select x0.*
      from my_table x0
     where x0.日期时间一起 in (select max(日期时间一起) from my_table group by A字段)但是怎样用sql把日期和时间连在一起呢?