有表如下, ID和Index是主键:
*Id char,
*Index int,
Time TIME,
Info char请问我怎么能用一条SQL语句选出表中
1)小于当前时间
2)每个ID只选择最大的一个Index的记录假设有数据如下,
Id, Index, Time, Info
1,1,T,123
1,2,T,234
2,1,T,1234
2,3,T,2345我用如下SQL不能选出第二行和第四行的Info列。
Select Id, max(Index) From TABLE Group By Id Having Time<now()
 
还请大家指教,先行多谢了。

解决方案 »

  1.   

    select a.* 
    from TABLE1 a,(
    select id,max(Index)  as mIndex
    from TABLE1
    where Time <now()) b
    where a.id=b.id and a.Index=b.mIndex当您的问题得到解答后请及时结贴.
    http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
      

  2.   

    一楼的应该还差点东西吧。出不来结果啊呵呵呵
    试试下面的,改了一下一楼的。select a.* from TABLE1 a,
    (select id, max(index1) as mIndex  from TABLE1
        where time1 < now() group by (id)) b
    where a.id = b.id and a.index1 = b.mIndex;
      

  3.   


    的确如楼上所说,少了个group by id.
      

  4.   

    非常感谢,一点小小的修改就可以了
    select a.* 
    from TABLE1 a,(
        select id,max(Index)  as mIndex
        from TABLE1
        where Time <now() Group By id) b
    where a.id=b.id and a.Index=b.mIndex