本文介绍了如何从 3 个不同类别中检索 3 个最新记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
就像我有如下表格:
是否可以从三个类别中获得三个帖子?请帮帮我.
Is it possible to get three posts from three category? Please help me.
推荐答案
You can take the last time stamp of per category -- using max
and group by
-- order他们按那个时间戳降序排列,并用 limit
取前三名.最后,使用 id
上的连接条件将此结果与原始记录连接起来,这样即使时间戳有关系,您也不会得到三个以上的结果,并输出这些结果.
You could take the last time stamp per category -- using max
and group by
-- order them by that time stamp in descending order and take the top three with limit
. Finally join this result with the original records using a join condition on id
so you will not get more than three results, even when there are ties on the time stamp, and output those.
tst
是这里的表名:
select tst.*
from tst
inner join (
select category,
max(time_stamp) time_stamp,
max(id) id
from tst
group by category
order by 2 desc
limit 3
) as topthree
on topthree.id = tst.id;
查看它在 rextester
这篇关于如何从 3 个不同类别中检索 3 个最新记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!