我有一个简单的问题;我认为会有一个简单的解决方案。想象一下这样的表:
Name Timestamp Data
Bob 2011-01-01 01:00:00 Hi
Alice 2011-02-02 02:00:00 Hello
Alice 2011-03-03 03:00:00 Hello
Bob 2011-04-04 04:00:00 Bye
Charlie 2011-05-05 05:00:00 Cheese
Charlie 2011-06-06 06:00:00 Toast
我想要的只是能够运行一个查询,显示每个Name的最新条目。所以在上面的表格中,我想要一个像这样的输出:
Name Timestamp Data
Bob 2011-04-04 04:00:00 Bye
Alice 2011-03-03 03:00:00 Hello
Charlie 2011-06-06 06:00:00 Toast
按数据订购。我无法弄清楚如何做到这一点。我虽然可以做到:
SELECT DISTINCT(Name), timestamp, Data FROM Table ORDER BY Data
但这不起作用。非常感激任何的帮助, 欢呼声。
答案 0 :(得分:1)
使用此查询获取最后一个唯一值,
SELECT * FROM (select * from `table` order by date DESC) t group by name
答案 1 :(得分:0)
select t1.* from table as t1
inner join (
select name,max(`timestamp`) as `timestamp`
from table
group by name) as t2
on t1.name = t2.name and t1.`timestamp` = t2.`timestamp`
答案 2 :(得分:0)
SELECT DISTINCT Name, Timestamp, Data
FROM Table
WHERE Timestamp =
(
SELECT MAX(Timestamp)
FROM Table AS tempTable
WHERE Table.Name = tempTable.Name
)