每天选择10行 [英] select 10 rows per day with order
本文介绍了每天选择10行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据库,其中记录了日期(时间戳) 我每天需要选择10条记录(每天还有更多记录) 并按几列对其进行排序...
i have a db with records with date (timestamp) i need to select 10 records for each day (there are many more per day) and order them by few columns...
该查询应如何显示?
推荐答案
您每天必须在子查询中每天获取10条记录,并通过左连接将它们连接到主表中,因此您将获得最大值每天10条记录. SQL看起来像这样:
You have to get your 10 records per day in a subquery for each day and join them to the main table by a left join, so you'll get max 10 records per day. The SQL would look like this:
SELECT t1.columns
FROM mytable t1
LEFT JOIN
(SELECT pk FROM mytable t2
WHERE t2.datecol = t1.datecol
ORDER BY t2.orderFor10Rows LIMIT 10) t3
ON t1.pk = t3.pk
ORDER BY t1.anyOtherColumns
我不习惯正确的MySQL语法,因此不予保证.
No warranty for proper MySQL-syntax as I'm not used to it.
这篇关于每天选择10行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文