假设有数据如下,
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()
还请大家指教,先行多谢了。
...全文
435打赏收藏
SQL请教
有表如下, 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
非常感谢,一点小小的修改就可以了
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
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;