小编典典

将查询结果限制为每组两行

sql

我有一个查询列出所有帖子:

SELECT *, DATE(FROM_UNIXTIME(`timestamp`)) `date` 
FROM `posts`
ORDER BY `date` DESC

查询列出所有行,我只希望每行显示最后2条帖子date,我该怎么做?


阅读 158

收藏
2021-04-14

共1个答案

小编典典

这可能有用,尽管我不能说太多性能。

SELECT DATE(FROM_UNIXTIME(MyTimestamp)) AS ForDate, *
FROM   MyPostsTable
WHERE  2 >=
(
    SELECT COUNT(*)
    FROM   MyPostsTable AS lookup
    WHERE  DATE(FROM_UNIXTIME(lookup.MyTimestamp)) = DATE(FROM_UNIXTIME(MyPostsTable.MyTimestamp))
    AND    lookup.MyTimeStamp >= MyPostsTable.MyTimestamp
)
2021-04-14