我有一个如下所示的表格-
我想要类别 1 中的一条最新记录、类别 2 中的两条最新记录和类别 4 中的三条最新记录,并重复此顺序。结果如下所示
最佳答案
对于您建议的输出,您可以执行以下操作:
(select t.*
from t
where category = 'category 1'
order by time_stamp desc
limit 1
) union all
(select t.*
from t
where category = 'category 2'
order by time_stamp desc
limit 2
) union all
(select t.*
from t
where category = 'category 4'
order by time_stamp desc
limit 3
)
order by category, time_stamp desc;
对于 3 个已知类别,union all
可能是 MySQL 中最简单的方法。
关于php - 如何从不同的特定类别中获取随机数量的最新记录并按类别排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46993509/