See SQLFiddle
使用 MAX()
select StackHistoryID, Max(TransactionDate)
from Stacks
group by StackHistoryID;
以及带ID的简单查询,使用MAX;(如果 ID 是增量的,即更高的 id 不能有更低的日期):
select Max(ID),StackHistoryID, Max(TransactionDate)
from Stacks
group by StackHistoryID;
如果您的 ID 不是与日期一起递增的,即您可以有更高的 ID 和更低的日期......然后你可以做类似的事情:
select alls.ID, s.StackHistoryID, s.TransactionDate
from
(
select StackHistoryID as StackHistoryID,
Max(TransactionDate) as TransactionDate
from stacks
group by StackHistoryID desc
) s, Stacks alls
where
alls.StackHistoryID = s.StackHistoryID
and
alls.TransactionDate = s.TransactionDate
order by StackHistoryID
;
See SQLFiddle