选择与SQLite中的条件匹配的最新行 [英] Select most recent rows that match a condition in SQLite
本文介绍了选择与SQLite中的条件匹配的最新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一张桌子:
Name, status, timestamp
我想选择与status ='active'匹配的行,但仅选择具有最新时间戳的行.因此,如果有这样的行:
And I want to select the rows that match status='active' but only those that have the most recent timestamp for each of them. So if there were rows like this:
Bob, active, 5/10/2010
Bob, active, 6/12/2010
Ann, inactive, 6/12/2000
Ann, active, 9/3/2009
Ann, active, 9/25/2010
我希望它返回:
Bob, active, 6/12/2010
Ann, active, 9/25/2010
我该怎么做?如果需要的话,我正在使用SQLite.
How can I do this? I'm using SQLite, if it matters.
谢谢.
推荐答案
select name, status, max(timestamp)
from my_table
where status = 'active'
group by name, status
看看 http://www.w3schools.com/sql/sql_groupby.asp
这篇关于选择与SQLite中的条件匹配的最新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文