2011-04-20 82 views
0

我有一個名爲'message'的簡單表,由'id'列和'date'列組成。可以有多個id具有相同的值。我正在查找每個ID最多返回三個的查詢,並且這三個查詢必須是日期最長的查詢。根據ID和日期返回多個組的結果

所以查詢會產生這樣的事:

id | date 
--- ------------------- 
36 2011-04-01 08:41:19 
36 2011-04-17 08:05:18 
36 2011-04-17 18:48:49 
39 2011-03-31 05:45:15 
39 2011-03-31 05:50:07 
39 2011-03-31 05:56:23 
41 2011-04-11 07:02:27 
41 2011-04-19 02:31:31 
41 2011-04-19 02:32:53 
etc... 

我一直有麻煩搞清楚了這一點。

回答

0
select * from table as t1 
where (select count(*) from table as t2 
     where t1.id = t2.id and t2.date > t1.date) < 3 
order by id, date desc 
+0

謝謝,它的效果很好。 – radios4rabbits 2011-04-20 13:46:44

相關問題